problem with global scope after package import via __init__.py

Denis S. Otkidach ods at fep.ru
Tue Aug 13 17:52:04 CEST 2002


On Tue, 13 Aug 2002, Mark Frazer wrote:

MF>
MF> I'd like to build a package out of several files as the
MF> individual files
MF> are large and become inwieldy.  Generally, I want a package
MF> user to
MF> import package
MF> only, and not worry about the individual files, so the
MF> __init__.py for
MF> the package does
MF> from file import *
MF> for all the files in the package.
MF>
MF> However, there seems to be a problem accessing scalars when
MF> the package
MF> is imported in this fashion.  The package files themselves
MF> can access the
MF> scalar properly, but the package user cannot.  Is this a
MF> bug?

fail.py:
#!/usr/bin/env python2

import p

val = 10
p.set_a (val)                     # set p.a.aval to 10 (p.aval
                                  # has its original value)

print "p.get_a() is ", p.get_a () # get p.a.aval
assert p.get_a () == val          # is OK
print "p.aval is ", p.aval
assert p.aval == val              # is not OK since p.aval is
                                  # -999 (the value of p.a.aval
                                  # at the moment when a is
                                  # imported) and val is 10

-- 
Denis S. Otkidach
http://www.python.ru/      [ru]
http://diveinto.python.ru/ [ru]





More information about the Python-list mailing list