dynamic creation of global Identifier

Alexander Eisenhuth newsuser at stacom-software.de
Wed Oct 11 05:11:13 EDT 2006


Hello alltogether,

I've a little problem in creating a new identifier in the global namespace. The 
following code creates a as local var in the namespace of init()

class A:
	def __init__(self, v):
		print "ctr of", self.__class__, "with", v
		self._v = v

def init():
	newIdentifier =  ["a", "b"]
	param = [1,2]
	for newId, par in zip(newIdentifier, param):
		exec "global %s" % newId
		exec "%s = A(par)" % newId

init()
print a, b

but why doesent exec "global ... create a identifier in the global namespace.

The next thing I want to do is to create a identifier in a packages namespace 
anyhow from where init() is called. How can I (or can I not) access from within 
a function the namespace of the "package" where it is defined?

Thanks a lot
Alexander




More information about the Python-list mailing list