473,406 Members | 2,378 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Module function can't see globals after import.


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
2 1379
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Berteun Damman | last post by:
Hello, First I was trying to get PyOSD, but as soon as I did `import pyosd' Python received a SIGABRT. Then I wrote my own module, which looks like: #include <Python.h> static PyMethodDef...
3
by: sean | last post by:
In article <cyH8c.3460$GH3.601@fed1read07>, sean@sands.beach.net (Sean Berry) writes: > I don't know that this partilarily bad programming, > but I was interested in doing the following. > > def...
4
by: Benjamin Rutt | last post by:
I'm trying to learn about introspection in Python. my ultimate goal is to be able to build a module "text database" of all modules that are in the sys.path, by discovering all candidate modules...
2
by: Reid Priedhorsky | last post by:
Dear group, I'd have a class defined in one module, which descends from another class defined in a different module. I'd like the superclass to be able to access objects defined in the first...
15
by: torched_smurf | last post by:
I have a java program in a package called 'cmd'. This of course conflicts with the builtin python package of the same name. The thing is, I need to be able to import from both of these packages in...
3
by: hollowspook | last post by:
def aa(): global b b=b+1 print b b=1 aa() The above code runs well in python shell.
6
by: pistacchio | last post by:
hi to all! can i load a module passing to it, automatically and as default, all the caller's global variables to act as module's global variables? thanks
1
by: Jeff Dyke | last post by:
I've come across an error that i'm not yet able to create a test case for but wanted to get see if someone could shed light on this. I have imported a module at the top of my file with import...
0
by: Fredrik Lundh | last post by:
Jeff Dyke wrote: so how did that processing use the "mymodulename" name? the calling method has nothing to do with what's considered to be a local variable in the method being called, so...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.