Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #104124

Re: Reason for not allowing import twice but allowing reload()

Newsgroups comp.lang.python
Date 2016-03-06 00:20 -0800
References <645cdd46-d4a4-49b3-a0d8-848608d70d73@googlegroups.com>
Message-ID <0886cfaa-b788-4e7d-b1b5-800c5988b7cf@googlegroups.com> (permalink)
Subject Re: Reason for not allowing import twice but allowing reload()
From alien2utoo@gmail.com

Show all headers | View raw


Based on the input from members, and my subsequent reading the textbook/tutorials, let
me summarize my understanding of "why subsequent imports of same module are designed to be effect-less".

1. Imports are costly affair, as it involves
 - finding the module's file
 - compile it to byte code (if needed)
 - run the module's code to build the objects it defines.

2a. Quoting from "Learning Python: Mark Lutz" : In Python, cross-file module linking
    is not resolved until such import statements are **executed at runtime**.

import being a statement, can come anywhere a statement can, so a module can be
selectively imported depending upon the conditions at runtime.

2b. Inter-dependencies between modules: For example, A (main module) imports B, C, D
    and E. B again imports C and E, D imports B and C.

If import statements are processed entirely from scratch each time, it would amount
to lot of **redundant (and costly)** work.

Secondly there could be dangerous? side-effects of reprocessing imports from scratch.

Let us say, in above example, module B does some initialization of variables, or
things like resetting a file for further work during the session. These variables and
file are updated during the course of execution of A.

Much later via some conditional path, module B is imported again, and BOOM (were
import processed from scratch again).

In this case - we are respecting that every import of a module should give same
picture (assuming it is not modified across two different invocation during the
program execution).

An argument here could be: to code module B in such a way that those initializations
don't happen again, but just once - check before resetting etc. during the import.
But wouldn't that violate the *same picture* requirement?

Probably my example is not that perfect.

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Reason for not allowing import twice but allowing reload() alien2utoo@gmail.com - 2016-02-28 22:40 -0800
  Re: Reason for not allowing import twice but allowing reload() Chris Angelico <rosuav@gmail.com> - 2016-02-29 18:01 +1100
    Re: Reason for not allowing import twice but allowing reload() Steven D'Aprano <steve@pearwood.info> - 2016-03-01 22:18 +1100
      Re: Reason for not allowing import twice but allowing reload() Chris Angelico <rosuav@gmail.com> - 2016-03-01 22:39 +1100
        Re: Reason for not allowing import twice but allowing reload() Steven D'Aprano <steve@pearwood.info> - 2016-03-02 04:11 +1100
          Re: Reason for not allowing import twice but allowing reload() Chris Angelico <rosuav@gmail.com> - 2016-03-02 05:04 +1100
      Re: Reason for not allowing import twice but allowing reload() Ian Kelly <ian.g.kelly@gmail.com> - 2016-03-01 14:53 -0700
      Re: Reason for not allowing import twice but allowing reload() Chris Angelico <rosuav@gmail.com> - 2016-03-02 09:02 +1100
      Re: Reason for not allowing import twice but allowing reload() Ian Kelly <ian.g.kelly@gmail.com> - 2016-03-01 15:29 -0700
        Re: Reason for not allowing import twice but allowing reload() Steven D'Aprano <steve@pearwood.info> - 2016-03-02 12:19 +1100
          Re: Reason for not allowing import twice but allowing reload() Ian Kelly <ian.g.kelly@gmail.com> - 2016-03-01 19:22 -0700
            Re: Reason for not allowing import twice but allowing reload() Rustom Mody <rustompmody@gmail.com> - 2016-03-02 02:15 -0800
              Re: Reason for not allowing import twice but allowing reload() Rustom Mody <rustompmody@gmail.com> - 2016-03-02 02:19 -0800
            Re: Reason for not allowing import twice but allowing reload() Grant Edwards <invalid@invalid.invalid> - 2016-03-02 15:15 +0000
      Re: Reason for not allowing import twice but allowing reload() Chris Angelico <rosuav@gmail.com> - 2016-03-02 11:13 +1100
  Re: Reason for not allowing import twice but allowing reload() Ian Kelly <ian.g.kelly@gmail.com> - 2016-02-29 00:02 -0700
  Re: Reason for not allowing import twice but allowing reload() Chris Angelico <rosuav@gmail.com> - 2016-02-29 18:11 +1100
    Re: Reason for not allowing import twice but allowing reload() BartC <bc@freeuk.com> - 2016-02-29 15:33 +0000
      Re: Reason for not allowing import twice but allowing reload() Chris Angelico <rosuav@gmail.com> - 2016-03-01 03:05 +1100
  Correct IDLE usage (was Reason for not allowing import twice but allowing reload()) Rustom Mody <rustompmody@gmail.com> - 2016-02-29 04:42 -0800
    Re: Correct IDLE usage (was Reason for not allowing import twice but allowing reload()) Terry Reedy <tjreedy@udel.edu> - 2016-03-01 01:52 -0500
      Re: Correct IDLE usage (was Reason for not allowing import twice but allowing reload()) Rustom Mody <rustompmody@gmail.com> - 2016-03-02 07:22 -0800
        Re: Correct IDLE usage (was Reason for not allowing import twice but allowing reload()) Terry Reedy <tjreedy@udel.edu> - 2016-03-02 21:40 -0500
          Re: Correct IDLE usage (was Reason for not allowing import twice but allowing reload()) Rustom Mody <rustompmody@gmail.com> - 2016-03-02 20:07 -0800
            Re: Correct IDLE usage (was Reason for not allowing import twice but allowing reload()) Rustom Mody <rustompmody@gmail.com> - 2016-03-02 20:17 -0800
  Re: Reason for not allowing import twice but allowing reload() alien2utoo@gmail.com - 2016-02-29 05:00 -0800
  Re: Reason for not allowing import twice but allowing reload() alien2utoo@gmail.com - 2016-02-29 05:22 -0800
    Re: Reason for not allowing import twice but allowing reload() alien2utoo@gmail.com - 2016-02-29 05:25 -0800
      Re: Reason for not allowing import twice but allowing reload() Steven D'Aprano <steve@pearwood.info> - 2016-03-02 04:00 +1100
        Re: Reason for not allowing import twice but allowing reload() alien2utoo@gmail.com - 2016-03-05 04:51 -0800
          Re: Reason for not allowing import twice but allowing reload() Steven D'Aprano <steve@pearwood.info> - 2016-03-10 00:53 +1100
    Reason for not allowing import twice but allowing reload() Rustom Mody <rustompmody@gmail.com> - 2016-02-29 05:51 -0800
      Re: Reason for not allowing import twice but allowing reload() alien2utoo@gmail.com - 2016-02-29 07:13 -0800
    Re: Reason for not allowing import twice but allowing reload() Terry Reedy <tjreedy@udel.edu> - 2016-03-01 02:04 -0500
  Re: Reason for not allowing import twice but allowing reload() alien2utoo@gmail.com - 2016-03-06 00:20 -0800
    Re: Reason for not allowing import twice but allowing reload() Steven D'Aprano <steve@pearwood.info> - 2016-03-07 01:50 +1100
  Re: Reason for not allowing import twice but allowing reload() alien2utoo@gmail.com - 2016-03-06 00:31 -0800

csiph-web