private variables/methods

Sean Ross sross at connectmail.carleton.ca
Sun Oct 12 13:36:41 EDT 2003


"Alex Martelli" <aleaxit at yahoo.com> wrote in message
news:Iseib.204801$hE5.6891483 at news1.tin.it...
> >> I think that __current_module__ is perhaps a bit too lengthy
> >
> > and redundant ;-)
>
> I disagree.  Lengthy it may be, but we do want a 'reserved module
> name' to use for this purpose.  For a normal import instruction to
> work, and to work just as well if you cut and paste the same function
> elsewhere, I think we want to define that "import somespecificname" is
> importing THIS module, the CURRENT module, under that name (or another
> specified with 'as').  This can be experimented with easily, by changing
> the builtin __import__ (or setting an import hook, maybe) in site-specific
> file; and if it catches on the builtin __import__ could easily be
customized
> to perform the same task quite speedily.

Hi.
I'm not sure I'm clear on what behaviour "import __current_module__" is
expected to have.
There's a bit more to follow but I'll ask my main question up front so we're
clear:

"Which of the following behaviours is preferred?"

I've been playing with some code, using my own pet global workaround, and
I'm not sure
if it's accomplishing the correct behaviour. The results are certainly not
the same as yours.
in fact, I think the behaviour of my version may be unexpected and dangerous
...

If inside module A you import a function f() from another module B that
tries to use the
current module directly, rather than global, to set variables and you call
that function
(i.e.,  B.f()),  my version will affect the values of A's global variables
whereas yours does
not. For example, here is some output from a test run of each method. The
code for all
of this will be posted at the bottom of this message. Note: I've added some
print statements
 to your code.


# Output of running setglobal1.py using import __current_module__
setglobal1.x = 23
ENTERING setglobal1.set_the_global()
global x = 23
set __current_module__.x = 45
global x = 45
EXITING setglobal1.set_the_global()
ENTERING setglobal2.set_the_global()
global x = 57
set __current_module__.x = 88
global x = 88
EXITING setglobal2.set_the_global()
setglobal1.x = 45


# Output of running main1.py using import __main__
main1.x = 23
ENTERING main1.foo()
global x = 23
set main.x = 45
global x = 45
EXITING main1.foo()
ENTERING main2.foo()
global x = 57
main2.x = 45
set main.x = 88
global x = 57
EXITING main2.foo()
main1.x = 88


As you can see, setglobal1.x = 45 in your version,  but my analogous
variable main1.x = 88.

Here's the code:

#==============================================
# site.py
import __builtin__, sys
_base_import = __builtin__.__import__
def __import__(name, *args):
    if name == '__current_module__':
        name = sys._getframe(1).f_globals['__name__']
    return _base_import(name, *args)
__builtin__.__import__ = __import__
# end of part to be executed once

#===============================================
# setglobal1.py

import setglobal2
x = 23

def set_the_global():
    print "ENTERING setglobal1.set_the_global()"
    import __current_module__
    global x
    print "global x = %s"%x
    __current_module__.x = 45
    print "set __current_module__.x = %s"%__current_module__.x
    print "global x = %s"%x
    print "EXITING setglobal1.set_the_global()"

if __name__ == "__main__":
    print "setglobal1.x = %s"%x
    set_the_global()
    setglobal2.set_the_global()
    print "setglobal1.x = %s"%x


#===============================================
# setglobal2.py
x = 57

def set_the_global():
    print "ENTERING setglobal2.set_the_global()"
    import __current_module__
    global x
    print "global x = %s"%x
    __current_module__.x = 88
    print "set __current_module__.x = %s"%__current_module__.x
    print "global x = %s"%x
    print "EXITING setglobal2.set_the_global()"

if __name__ == "__main__":
    print "setglobal2.x = %s"%x
    set_the_global()
    print "setglobal2.x = %s"%x




# And now my version:

#===============================================
# main1.py
import main2

x = 23

def foo():
    print "ENTERING main1.foo()"
    import __main__ as main
    global x
    print "global x = %s"%main.x
    main.x = 45
    print "set main.x = %s"%main.x
    print "global x = %s"%x
    print "EXITING main1.foo()"

if __name__ == "__main__":
    print "main1.x = %s"%x
    foo()
    main2.foo()
    print "main1.x = %s"%x



#===============================================
# main2.py
x = 57

def foo():
    print "ENTERING main2.foo()"
    import __main__ as main
    global x
    print "global x = %s"%x
    print "main2.x = %s"%main.x
    main.x = 88
    print "set main.x = %s"%main.x
    print "global x = %s"%x
    print "EXITING main2.foo()"

if __name__ == "__main__":
    print "main2.x = %s"%x
    foo()
    print "main2.x = %s"%x



Thanks for your time,
Sean






More information about the Python-list mailing list