Re: setattr in class

2008-09-12 Thread Michael Palmer
On Sep 12, 11:08 am, Bojan Mihelac <[EMAIL PROTECTED]> 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) # t

Re: setattr in class

2008-09-12 Thread Arnaud Delobelle
On Sep 12, 4:30 pm, Bojan Mihelac <[EMAIL PROTECTED]> wrote: > On Sep 12, 5:21 pm, Christian Heimes <[EMAIL PROTECTED]> wrote: > > > 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 c

Re: setattr in class

2008-09-12 Thread Bojan Mihelac
On Sep 12, 5:35 pm, Wojtek Walczak <[EMAIL PROTECTED]> wrote: > On Fri, 12 Sep 2008 08:08:18 -0700 (PDT), 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)

Re: setattr in class

2008-09-12 Thread Wojtek Walczak
On Fri, 12 Sep 2008 08:08:18 -0700 (PDT), 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) #

Re: setattr in class

2008-09-12 Thread Bojan Mihelac
On Sep 12, 5:21 pm, Christian Heimes <[EMAIL PROTECTED]> wrote: > 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 i

Re: setattr in class

2008-09-12 Thread Christian Heimes
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 --

Re: setattr in class

2008-09-12 Thread Fredrik Lundh
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 wo

setattr in class

2008-09-12 Thread 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 p