dynamic creation of global Identifier

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Alexander Eisenhuth

    #1

    dynamic creation of global Identifier

    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(newIdentifi er, 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

  • Theerasak Photha

    #2
    Re: dynamic creation of global Identifier

    On 10/11/06, Alexander Eisenhuth <newsuser@staco m-software.dewrot e:
    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__.__d ict__['foo'] = 42

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

    -- Theerasak

    Comment

    • Steve Holden

      #3
      Re: dynamic creation of global Identifier

      Theerasak Photha wrote:
      On 10/11/06, Alexander Eisenhuth <newsuser@staco m-software.dewrot e:
      >
      >
      >>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(newIdentifi er, 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<__ma in__.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

      Comment

      Working...