Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Dynamic property names on class

Reply
Thread Tools

Dynamic property names on class

 
 
Bryan
Guest
Posts: n/a
 
      11-13-2009
I have several properties on a class that have very similar behavior.
If one of the properties is set, all the other properties need to be
set to None. So I wanted to create these properties in a loop like:

class Test(object):
for prop in ['foo', 'bar', 'spam']:
# Attribute that data is actually stored in
field = '_' + prop
# Create getter/setter
def _get(self):
return getattr(self, field)
def _set(self, val):
setattr(self, field, val)
for otherProp in prop:
if otherProp != prop: setattr(self, '_' + otherProp, None)
# Assign property to class
setattr(Test, prop, property(_get, _set))

t = Test()
t.foo = 1
assert t.bar == t.spam == None

But the class Test is not defined yet, so I can't set a property on
it. How can I do this?
 
Reply With Quote
 
 
 
 
Diez B. Roggisch
Guest
Posts: n/a
 
      11-13-2009
Bryan schrieb:
> I have several properties on a class that have very similar behavior.
> If one of the properties is set, all the other properties need to be
> set to None. So I wanted to create these properties in a loop like:
>
> class Test(object):
> for prop in ['foo', 'bar', 'spam']:
> # Attribute that data is actually stored in
> field = '_' + prop
> # Create getter/setter
> def _get(self):
> return getattr(self, field)
> def _set(self, val):
> setattr(self, field, val)
> for otherProp in prop:
> if otherProp != prop: setattr(self, '_' + otherProp, None)
> # Assign property to class
> setattr(Test, prop, property(_get, _set))
>
> t = Test()
> t.foo = 1
> assert t.bar == t.spam == None
>
> But the class Test is not defined yet, so I can't set a property on
> it. How can I do this?


With a metaclass, or a post-class-creation function. Which is a
metaclass without being fancy.

Just put your above code into a function with the class in question as
argument, and invoke it after Test is defined.

Diez
 
Reply With Quote
 
 
 
 
Bryan
Guest
Posts: n/a
 
      11-13-2009
On Nov 13, 9:34*am, "Diez B. Roggisch" <de...@nospam.web.de> wrote:
> Bryan schrieb:
>
>
>
> > I have several properties on a class that have very similar behavior.
> > If one of the properties is set, all the other properties need to be
> > set to None. *So I wanted to create these properties in a loop like:

>
> > class Test(object):
> > * *for prop in ['foo', 'bar', 'spam']:
> > * * * * * *# Attribute that data is actually stored in
> > * * * * * *field = '_' + prop
> > * * * * * *# Create getter/setter
> > * * * * * *def _get(self):
> > * * * * * * * * * *return getattr(self, field)
> > * * * * * *def _set(self, val):
> > * * * * * * * * * *setattr(self, field, val)
> > * * * * * * * * * *for otherProp in prop:
> > * * * * * * * * * * * * * *if otherProp != prop: setattr(self, '_' + otherProp, None)
> > * * * * * *# Assign property to class
> > * * * * * *setattr(Test, prop, property(_get, _set))

>
> > t = Test()
> > t.foo = 1
> > assert t.bar == t.spam == None

>
> > But the class Test is not defined yet, so I can't set a property on
> > it. *How can I do this?

>
> With a metaclass, or a post-class-creation function. Which is a
> metaclass without being fancy.
>
> Just put your above code into a function with the class in question as
> argument, and invoke it after Test is defined.
>
> Diez


I think there are some closure issues with this as I am getting very
strange results. I think all properties have the getter/setters of
whatever the last item in the list was.
t.foo = 'settingFoo' actually sets t.spam, as 'spam' was the last
property generated.
 
Reply With Quote
 
Diez B. Roggisch
Guest
Posts: n/a
 
      11-13-2009
Bryan schrieb:
> On Nov 13, 9:34 am, "Diez B. Roggisch" <de...@nospam.web.de> wrote:
>> Bryan schrieb:
>>
>>
>>
>>> I have several properties on a class that have very similar behavior.
>>> If one of the properties is set, all the other properties need to be
>>> set to None. So I wanted to create these properties in a loop like:
>>> class Test(object):
>>> for prop in ['foo', 'bar', 'spam']:
>>> # Attribute that data is actually stored in
>>> field = '_' + prop
>>> # Create getter/setter
>>> def _get(self):
>>> return getattr(self, field)
>>> def _set(self, val):
>>> setattr(self, field, val)
>>> for otherProp in prop:
>>> if otherProp != prop: setattr(self, '_' + otherProp, None)
>>> # Assign property to class
>>> setattr(Test, prop, property(_get, _set))
>>> t = Test()
>>> t.foo = 1
>>> assert t.bar == t.spam == None
>>> But the class Test is not defined yet, so I can't set a property on
>>> it. How can I do this?

>> With a metaclass, or a post-class-creation function. Which is a
>> metaclass without being fancy.
>>
>> Just put your above code into a function with the class in question as
>> argument, and invoke it after Test is defined.
>>
>> Diez

>
> I think there are some closure issues with this as I am getting very
> strange results. I think all properties have the getter/setters of
> whatever the last item in the list was.
> t.foo = 'settingFoo' actually sets t.spam, as 'spam' was the last
> property generated.


That's a FAQ. Closures capture the *names*, not the values. There are
various options to remedy this, e.g. by something like this:


def gen_property(prop):

def _get(...) # your code


return property(_get, _set)

setattr(Test, prop, gen_property(prop))


Diez
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Dynamic class names Martin Boese Ruby 4 04-02-2008 12:15 PM
OO Javascript - Class.Prototype.Property vs. This.Property Spam Catcher Javascript 4 11-21-2007 05:23 PM
Nested Class, Member Class, Inner Class, Local Class, Anonymous Class E11 Java 1 10-12-2005 03:34 PM
Converting 'flat' gate level names to hierarchical names Paddy McCarthy VHDL 3 09-24-2004 05:34 PM
member function names identical to class names Ares Lagae C++ 8 09-24-2004 11:23 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57