confused by isinstance()

Francois Petitjean littlejohn.75 at free.fr
Fri Feb 1 16:39:31 EST 2002


>> (1) It is trivial as it has :
>> only one module
>Two modules, Foo and __main__
yes, technically, but superficially a newbie can see only one.
>> only one class
>Two classes, one in each module. Foo.Foo and __main__.Foo
as a consequence of above

>When you run a script, it executes the code in the context of the module
>called __main__. When you import a module it has a separate context even if
>the script and the module came out of the same source file. Importing a
>module executes each line in the file, even if you already ran most of them
>as a script.

I suppose that this context corresponds to a namespace, which is implemented
internally by a dict.
Where in the documentation is explained what means "Importing a module
executes each line in the file". The lines are syntaxically checked, sure,
but most of the work is not done at this stage:
def curve(x):
    return return 10.0*math.sin(x)/x
conversion to byte code.

>Every time you execute a class statement, even if the class name is the
>same, you create a new class.
This is the crux of the problem, but after the first import, it seems that
successive import of a module give the same class, due to the sys.module
caching. Right?

So, if I want to be on the safe side and to test a module Foo as it appears
from outside, I can begin the __main__ part with import Foo. In this case,
instanciation is of the form x = Foo.Foo(), all is good except for the
doubling of the memory to store the class.

Thank you for your insightful post.





More information about the Python-list mailing list