Confused about properties, descriptors, and attributes...

Alexis Roda arv.nntp at gmail.com
Thu Apr 20 18:53:56 EDT 2006


redefined.horizons at gmail.com escribió:
> I've been reading about Python Classes, and I'm a little confused about
> how Python stores the state of an object. I was hoping for some help.
> 
> I realize that you can't create an empty place holder for a member
> variable of a Python object. It has to be given a value when defined,
> or set within a method.

I don't understand what do you mean with "empty place holder". Some 
pre-allocated-and-uninitialized memory to hold the attribute value (ala 
C++ o java)? Maybe slots?

 >>> class Foo(object) :
...   __slots__ = ['foo']
...
 >>> f=Foo()
 >>> dir(f)
['__class__', '__delattr__', '__doc__', '__getattribute__', '__hash__', 
'__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', 
'__repr__', '__setattr__', '__slots__', '__str__', 'foo']
 >>> f.foo
Traceback (most recent call last):
   File "<stdin>", line 1, in ?
AttributeError: foo
 >>> f.foo = 3
 >>> dir(f)
['__class__', '__delattr__', '__doc__', '__getattribute__', '__hash__', 
'__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', 
'__repr__', '__setattr__', '__slots__', '__str__', 'foo']
 >>> f.foo
3

> But what is the difference between an Attribute of a Class, a
> Descriptor in a Class, and a Property in a Class?

I don't know what the formal definitions are. Probably "attribute" 
refers to anything that can be obtained by way of getattr(). According 
to this "definition" an attribute does not require to be stored in the 
instance, it can be calculated or obtained from somewhere else.

 >>> class SimpleExample :
...   def __getattr__(self, name) :
...     if name=='attr' :
...       return 0
...     raise AttributeError(name)
...
 >>> s = SimpleExample()
 >>> s.attr
0
 >>> s.bar
Traceback (most recent call last):
   File "<stdin>", line 1, in ?
   File "<stdin>", line 5, in __getattr__
AttributeError: bar


descriptors are a mechanism/protocol for attribute access. It's intended 
to solve some problems of the original __getattr__() protocol.

A property is a specialized type of descriptor which allows 
customization of attribute's access methods.

> If I had a Monster Class, and I wanted to give each Monster a member
> variable called ScaryFactor, how would I define it?

If you're asking when to use plain attributes, descriptors or properties 
the answer is that it depends on what you want/need:

* if you store monsters in a database and the ScaryFactor attribute is 
computed by some complex and expensive database lookup I'll go for some 
kind of lazy descriptor/property

* if you want to be able to control/limit what can be done with an 
attribute may be a property is enough. This includes things like make 
the attribute read only etc.

* if you don't have special requirements a plain attribute would be 
probably simpler and faster


>  Does a Property
> simply provide methods that access an Attribute?

not exactly, it allows you to assign/define the methods that will be 
used to access the attribute, but you still access to the attribute in 
the usual "dot-way".


For further info take a look at:

http://www.python.org/doc/newstyle/


HTH



More information about the Python-list mailing list