global namescape of multilevel hierarchy

Ben Cartwright bencvt at gmail.com
Mon Mar 13 20:05:07 EST 2006


Sakcee wrote:
> 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




More information about the Python-list mailing list