built-in 'property'

Shalabh Chaturvedi shalabh at cafepy.com
Tue Dec 28 02:11:21 CET 2004


Bob.Cowdery at CGI-Europe.com wrote:
> 
> Hi
>  
> Can any one explain how property works. It seems to be fine if executed 
> on import i.e. if the property statement is at class scope. 

Properties are meant to be used at the class scope. A property is a kind 
of descriptor. See http://users.rcn.com/python/download/Descriptor.htm 
for details of descriptors.


> If I put the 
> statement inside __init__() then it appears to work ok but when I try to 
> access the property by e.g. klass.x it just tells me it is a property 
> object.

Please show us the code of what you are doing and describe what you are 
trying to do. There are a lot many things one could do with a property 
in __init__(). Mostly properties are just defined at the class scope. 
And rarely would you access klass.x (where x is a property object), 
mostly you would do instance.x.

> Is this statement only designed to work at class scope? I really 
> want to set these properties per instance.

Again, what exactly are you trying to do? The link above has some simple 
examples of how to use properties.

--
Shalabh




More information about the Python-list mailing list