setattr in class

B

Bojan Mihelac

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
 
F

Fredrik Lundh

Bojan said:
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:
.... 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>
 
B

Bojan Mihelac

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?
 
B

Bojan Mihelac

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
I guess A class not yet exists in line 4. Is it possible to achive
adding dynamic attributes without using exec?

Yes, it is:

-------------
class A:
   for i in (1, 2):
      locals()['xxx%d' % (i)] = i

print A.xxx1
print A.xxx2
a = A()
print a.xxx1
print a.xxx2
-------------

And the output is:

-----
1
2
1
2

thnx Wojtek, this works!
 
A

Arnaud Delobelle

thanks, can you give example on using a metaclass?

class MoreMeta(type):
def __init__(self, name, bases, attrs):
more = attrs.get('moreattrs')
if more:
for attr, val in more.iteritems():
setattr(self, attr, val)

class MoreObject(object):
__metaclass__ = MoreMeta

class A(MoreObject):
moreattrs = {}
for i in '12':
moreattrs['title_' + i] = int(i)
 
M

Michael Palmer

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.DictMixin.
 

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

Forum statistics

Threads
473,772
Messages
2,569,593
Members
45,111
Latest member
KetoBurn
Top