Bug in Python class static variable?

Bruza benruza at gmail.com
Tue Jul 3 11:54:55 CEST 2007


On Jul 2, 1:21 pm, Duncan Booth <duncan.bo... at invalid.invalid> wrote:
> Bruza <benr... at gmail.com> wrote:
> > On Jul 2, 3:52 am, Duncan Booth <duncan.bo... at invalid.invalid> wrote:
> >> Bruza <benr... at gmail.com> wrote:
> >> > I am trying to define a class static variable. But the value of the
> >> > static variable seems to be only defined inside the file that the
> >> > class is declared. See the code below. When I run "python w.py", I
> >> > got:
>
> >> When you run "python w.py" the *script* w.py is loaded as the module
> >> __main__. Importing a module called 'w' creates a new module which is
> >> unrelated to __main__. If you want access to variables defined in the
> >> main script then you need to import __main__.
>
> >> Don't use 'from module import *':
>
> >> The import statements are executed when the interpreter reaches them
> >> in the source. Even if you fix your code to import from __main__, the
> >> values you try to import from __main__ won't exist when the import
> >> statement executes: the first 'from a import *' will load and execute
> >> all of module 'a', but when that executes 'from __main__ import *' it
> >> just imports names defined in the main script *before* a was
> >> imported.
>
> >> In general, don't try to do this: put all your classes into modules
> >> and just put minimal startup code into a script.
>
> > Duncan,
>
> > Thanks for replying. However, I am still confused...
> > Even if I put "from __main__ import *" in both "a.py" and "w.py", I
> > still got
> > the same results. So, how should I re-structure my program to make the
> > class
> > static variable works???
>
> Option 1:
> In w.py put:
>
> import a
>
> and then refer to a.ClassA
>
> In a.py put:
>
> import __main__
>
> and then refer to __main__.ClassW
>
> But better, option 2:
>
> Create a new file script.py which contains:
>
> import w
> if __name__=='__main__':
>     w.startup()
>
> then in a use 'import w' and in w use 'import a' and refer to a.ClassA and
> w.ClassW as above.
>
> Try to think through the order in which Python interprets your code:
> remember everything is interpreted. Both 'import' statements and 'class'
> statements are really just variation on an assignment, so none of the names
> exist until the lines which declare them have been executed. A line 'import
> a' is roughly the same as:
>
>    a = __import__(something)
>
> and a statement such as 'class ClassA: whatever' is roughly the same as:
>
>   ClassA = type('ClassA', baseclasses, dict)
>
> You should never attempt to use the 'from X import *' form of import when
> you have modules which include each other (you might get away with it if
> you move the imports to the end of the module instead of the beginning, but
> it is much simpler just to import the modules, and good practice in any
> case).

Duncan,

Thanks for the reply; both approaches worked!! Looks like the problem
is because
of "ClassW" is defined in the default "__main__" module, not the "w"
module as
I thought.

I also figured out a 3rd approach adding the following code in "w.py".
The idea
is that I force the program to import "w.py" as "w" if "w.py" is
loaded into
"__main__". Then I can run the qualified "w.startup()". And it also
works :-)...

if __name__ == '__main__':
    import w
    w.startup()




More information about the Python-list mailing list