Using python, you can set the instance attribute using one of the following two methods:
>>> class Foo(object):
pass
>>> a = Foo()
>>> a.x = 1
>>> a.x
1
>>> setattr(a, 'b', 2)
>>> a.b
2
You can also assign properties through the property decorator.
>>> class Bar(object):
@property
def x(self):
return 0
>>> a = Bar()
>>> a.x
0
My question is, how can I assign a property to an instance?
My intuition was to try something like this ...
>>> class Doo(object):
pass
>>> a = Doo()
>>> def k():
return 0
>>> a.m = property(k)
>>> a.m
<property object at 0x0380F540>
... but I get this strange property object. Similar experiments yielded similar results. I assume that properties are more closely related to classes than instances in some respects, but I don’t know that the inner workings are good enough to understand what is going on here.
source
share