global namescape of multilevel hierarchy

S

Sakcee

Hi

I have a script e.g.

import package.module

ID = "55"

package.module.checkID()



now in package.module.checkID function, i wnat to know what is the ID
defiend in the calling scriipt


if I dot globals(), it returns only items in module. is there a way
to get the script level namesapce


thanks
 
B

Ben Cartwright

Sakcee said:
now in package.module.checkID function, i wnat to know what is the ID
defiend in the calling scriipt

It's almost always a really bad idea to kludge scopes like this. If
you need to access a variable from the caller's scope in a module
function, make it an argument to that function. That's what arguments
are for in the first place!

But, if you must, you can use the inspect module:

import inspect
def checkID():
ID = inspect.currentframe().f_back.f_locals['ID']
print ID

--Ben
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top