overriding a property

Steven D'Aprano steve at REMOVE-THIS-cybersource.com.au
Tue Oct 19 12:28:31 EDT 2010


On Tue, 19 Oct 2010 06:39:56 -0700, Lucasm wrote:

> Hi,
> 
> A question. Is it possible to dynamically override a property?
> 
> class A(object):
>     @property
>     def return_five(self):
>         return 5
> 
> I would like to override the property for an instance of A to say the
> string 'bla'.


>>> class A(object):
...     _five = 5  # class attribute shared by all instances
...     @property
...     def return_five(self):
...         return self._five
...
>>>
>>> a = A()
>>> a._five = 'bla'  # set an instance attribute
>>> b = A()
>>> print a.return_five
bla
>>> print b.return_five
5



-- 
Steven



More information about the Python-list mailing list