semi-newbie module namespace confusion

Fredrik Lundh fredrik at pythonware.com
Tue Oct 4 04:17:23 EDT 2005


David Murmann wrote:

> I ran into the same problem some time ago and even wanted to post here
> about it, but found out that it had been reported as a bug three times
> at sourceforge (if i remember correctly). The comments there explained
> it of course, but I still think that this behavior is somehow "wrong".
>
> I like to think of the import statement as a way to provide the names
> defined in a module to the current namespace, so there is no "this gets
> evaluated twice".

are you sure you understand the problem?  import does exactly what you
say; it creates a module object and populates it by running the code in the
module.

the problem is that when you hand Python a chunk of code (a script), it
doesn't necessarily know where it came from.  and even if you know the
filename it came from, there's no way to know if that chunk actually corre-
sponds to a module somewhere out there (the import system can map a
module name to a file, but it cannot map a file to a module name).

> Now i wonder how difficult it would be to "correct" the behavior?

there's no way to "fix" it without introducing a huge number of inconsistencies.

> And would such a change break any code?

any code that uses the "if __name__ == "__main__" pydiom, for a start.

</F> 






More information about the Python-list mailing list