How do I assign a property to an instance in Python?
问题 Using python, one can set an attribute of a instance via either of the two methods below: >>> class Foo(object): pass >>> a = Foo() >>> a.x = 1 >>> a.x 1 >>> setattr(a, 'b', 2) >>> a.b 2 One can also assign properties via 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 >