By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,994 Members | 1,285 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,994 IT Pros & Developers. It's quick & easy.

Module function can't see globals after import.

P: n/a

Using this example:

[in mymodule]

def whatisx():
try:
print 'x is',x
except:
print 'There is no x'
[in mymain]

from mymodule import printx
x = 10
whatisx()

prints -> There is no x
Is there a way to tell the imported function printx to use mymain's
globals instead of it's own copy without passing it as an argument?

I'm experimenting with a function that limits both the reading and
writing of globals and builtins to only specific names. But after
it's imported it has it's own globals so it only works if it's copied
to the current module.
def howareyou():
allow_globals( rw_globals='y', r_globals='x', builtins='int')
global y # This is still needed.
# ... lots more code.
y = int(x*y)
return 'I am fine.'

x = 5
y = 10
print howareyou()
This function has read/write access to y, can only read x, and has
only read access to int(). It will generate an error if an attempt is
made to use anything other than what is specified.

Using:
allow_globals() # No globals, and all builtins.
allow_globals(builtins=None) # No globals, and no builtins.

Still have a few minor issues to work out. Like getting it to work
correctly after it's imported. :/

Ron_Adam







Jul 18 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
On Mon, 28 Mar 2005 17:19:42 GMT, rumours say that Ron_Adam
<ra****@tampabay.rr.com> might have written:
Is there a way to tell the imported function printx to use mymain's
globals instead of it's own copy without passing it as an argument?


No, but if you insist on working with globals for some reason of your
own, in the module you can:

import __main__

and access the main modules globals (eg 'x') as:

__main__.x
--
TZOTZIOY, I speak England very best.
"Be strict when sending and tolerant when receiving." (from RFC1958)
I really should keep that in mind when talking with people, actually...
Jul 18 '05 #2

P: n/a
On Mon, 28 Mar 2005 20:46:32 +0300, Christos "TZOTZIOY" Georgiou
<tz**@sil-tec.gr> wrote:
On Mon, 28 Mar 2005 17:19:42 GMT, rumours say that Ron_Adam
<ra****@tampabay.rr.com> might have written:
Is there a way to tell the imported function printx to use mymain's
globals instead of it's own copy without passing it as an argument?
No, but if you insist on working with globals for some reason of your
own, in the module you can:


Actually I avoid them where ever and when ever possible. ;)

This is more of an attempt to help find and prevent problems of that
sort. I've started to put together a took kit package of sorts for
finding and preventing problems. It needs to look at the name spaces
to work correctly.
import __main__

and access the main modules globals (eg 'x') as:

__main__.x


Thanks, 'That worked fine.! :)

Jul 18 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.