No Thoughts about Everything

Keel Thaan kmt at MailCentro.zzn.com
Thu Feb 26 00:21:52 CET 2004


Josiah Carlson wrote:
>> This will create c as an unmodifiable attribute:
>>
>>     c = property(lambda self: 2.99792458e8)
>>
>>
>> Does that do what you want?
> 
> 
>  >>> class foo:
> ...     c = property(lambda self: 2.99792458e8)
> ...
>  >>> v = foo()
>  >>> v.c
> 299792458.0
>  >>> v.c += 1
>  >>> v.c
> 299792459.0
>  >>> foo().c
> 299792458.0
>  >>>
> 
> It doesn't quite work.

I suppose I should have mentioned that you have to use a new-style class:

 >>> class foo(object):
...     c = property(lambda self: 2.99792458e8)
...
 >>> v = foo()
 >>> v.c
299792458.0
 >>> v.c += 1
Traceback (most recent call last):
   File "<input>", line 1, in ?
AttributeError: can't set attribute


I don't think the behavior of property() is even officially defined for 
old-style classes.




More information about the Python-list mailing list