dynamic creation of global Identifier

  • Thread starter Alexander Eisenhuth
  • Start date
A

Alexander Eisenhuth

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
 
T

Theerasak Photha

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

I haven't had much use for exec, but it operates in its own, more or
less cloistered namespace. It can't set globals among other things.

You can frob the globals like so

import __builtin__
__builtin__.__dict__['foo'] = 42

But by the time you get there, it is almost certainly time to refactor.

-- Theerasak
 
S

Steve Holden

Theerasak said:
I haven't had much use for exec, but it operates in its own, more or
less cloistered namespace. It can't set globals among other things.
Well that's not strictly true:
{'__builtins__': <module '__builtin__' (built-in)>, '__name__':
'__main__', '__file__': '/c/Steve/.pythonrc', 'sys': <module 'sys'
(built-in)>, '__doc__': None}{'__builtins__': <module '__builtin__' (built-in)>, '__file__':
'/c/Steve/.pythonrc', 'sys': <module 'sys' (built-in)>, 'NEW': 42,
'__name__': '__main__', '__doc__': None}

The problem was that the two exec statements were being treated
separately because they use independent execution contexts. Make them
the same, and bingo!

sholden@bigboy ~/Projects/Python
$ cat test38.py
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
%s = A(par)""" % (newId, newId)

init()
print a, b



sholden@bigboy ~/Projects/Python
$ python test38.py
ctr of __main__.A with 1
ctr of __main__.A with 2
<__main__.A instance at 0x186c6d2c> <__main__.A instance at 0x186c6e0c>

However, none of this will necessarily get the OP further, since the
interpretation of the global statement will be as "global to the module
that it appears in".

regards
Steve
 

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,774
Messages
2,569,599
Members
45,162
Latest member
GertrudeMa
Top