setattr in class

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Bojan Mihelac

    setattr in class

    Hi all - when trying to set some dynamic attributes in class, for
    example:

    class A:
    for lang in ['1', '2']:
    exec('title_%s = lang' % lang) #this work but is ugly
    # setattr(A, "title_%s" % lang, lang) # this wont work

    setattr(A, "title_1", "x") # this work when outside class

    print A.title_1
    print A.title_2

    I guess A class not yet exists in line 4. Is it possible to achive
    adding dynamic attributes without using exec?

    thanks,
    Bojan
  • Fredrik Lundh

    #2
    Re: setattr in class

    Bojan Mihelac wrote:
    Hi all - when trying to set some dynamic attributes in class, for
    example:
    >
    class A:
    for lang in ['1', '2']:
    exec('title_%s = lang' % lang) #this work but is ugly
    # setattr(A, "title_%s" % lang, lang) # this wont work
    >
    setattr(A, "title_1", "x") # this work when outside class
    >
    print A.title_1
    print A.title_2
    >
    I guess A class not yet exists in line 4. Is it possible to achive
    adding dynamic attributes without using exec?
    Move the for-in loop out of the class definition:
    >>class A:
    .... pass
    ....
    >>for lang in ['1', '2']:
    .... setattr(A, "title_%s" % lang, lang)
    >>a = A()
    >>a.title_1
    '1'

    A truly dynamic solution (using __getattr__ and modification on access)
    would probably give you a more "pythonic" solution.

    </F>

    Comment

    • Christian Heimes

      #3
      Re: setattr in class

      Bojan Mihelac wrote:
      I guess A class not yet exists in line 4. Is it possible to achive
      adding dynamic attributes without using exec?
      Correct, the class doesn't exist until the end of the class body. You
      can either do it outside the class definition or you can use a metaclass.

      Christian

      Comment

      • Bojan Mihelac

        #4
        Re: setattr in class

        On Sep 12, 5:21 pm, Christian Heimes <li...@cheimes. dewrote:
        Bojan Mihelac wrote:
        I guess A class not yet exists in line 4. Is it possible to achive
        adding dynamic attributes without using exec?
        >
        Correct, the class doesn't exist until the end of the class body. You
        can either do it outside the class definition or you can use a metaclass.
        >
        Christian
        thanks, can you give example on using a metaclass?

        Comment

        • Arnaud Delobelle

          #5
          Re: setattr in class

          On Sep 12, 4:30 pm, Bojan Mihelac <bmihe...@gmail .comwrote:
          On Sep 12, 5:21 pm, Christian Heimes <li...@cheimes. dewrote:
          >
          Bojan Mihelac wrote:
          I guess A class not yet exists in line 4. Is it possible to achive
          adding dynamic attributes without using exec?
          >
          Correct, the class doesn't exist until the end of the class body. You
          can either do it outside the class definition or you can use a metaclass.
          >
          Christian
          >
          thanks, can you give example on using a metaclass?
          class MoreMeta(type):
          def __init__(self, name, bases, attrs):
          more = attrs.get('more attrs')
          if more:
          for attr, val in more.iteritems( ):
          setattr(self, attr, val)

          class MoreObject(obje ct):
          __metaclass__ = MoreMeta

          class A(MoreObject):
          moreattrs = {}
          for i in '12':
          moreattrs['title_' + i] = int(i)
          >>A.title_1
          1
          >>A.title_2
          2
          >>>
          --
          Arnaud

          Comment

          • Michael Palmer

            #6
            Re: setattr in class

            On Sep 12, 11:08 am, Bojan Mihelac <bmihe...@gmail .comwrote:
            Hi all - when trying to set some dynamic attributes in class, for
            example:
            >
            class A:
            for lang in ['1', '2']:
            exec('title_%s = lang' % lang) #this work but is ugly
            # setattr(A, "title_%s" % lang, lang) # this wont work
            >
            setattr(A, "title_1", "x") # this work when outside class
            >
            print A.title_1
            print A.title_2
            >
            I guess A class not yet exists in line 4. Is it possible to achive
            adding dynamic attributes without using exec?
            >
            thanks,
            Bojan
            Is it really worth it? If the names of the attributes are only known
            at runtime, why not just use a dict - that's what they are for. If you
            want a dict plus some special behaviour, just write a class that
            inherits from dict, or use UserDict.DictMi xin.

            Comment

            Working...