By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,908 Members | 1,892 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,908 IT Pros & Developers. It's quick & easy.

dynamic creation of global Identifier

P: n/a
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

Oct 11 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
On 10/11/06, Alexander Eisenhuth <ne******@stacom-software.dewrote:
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
Oct 11 '06 #2

P: n/a
Theerasak Photha wrote:
On 10/11/06, Alexander Eisenhuth <ne******@stacom-software.dewrote:

>>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.
Well that's not strictly true:
>>globals()
{'__builtins__': <module '__builtin__' (built-in)>, '__name__':
'__main__', '__file__': '/c/Steve/.pythonrc', 'sys': <module 'sys'
(built-in)>, '__doc__': None}
>>exec "NEW = 42"
globals()
{'__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
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://holdenweb.blogspot.com
Recent Ramblings http://del.icio.us/steve.holden

Oct 11 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.