Method / Functions - What are the differences?

John Posner jjposner at optimum.net
Thu Mar 4 16:17:20 EST 2010


On 3/3/2010 6:56 PM, John Posner wrote:
>> ... I was thinking
> today about "doing a Bruno", and producing similar pieces on:
>> * properties created with the @property decorator
>> * the descriptor protocol
>> I'll try to produce something over the next couple of days.
>
Starting to think about a writeup on Python properties, I've discovered 
that the official Glossary [1] lacks an entry for "property" -- it's 
missing in both Py2 and Py3!
Here's a somewhat long-winded definition -- comments, please:
---------------------------------------------
An attribute, *a*, of an object, *obj*, is said to be implemented as a 
property if the standard ways of accessing the attribute:
 * evaluation: print obj.a
 * assignment: obj.a = 42
 * deletion: del obj.a
... cause methods of a user-defined *property object* to be invoked. The 
attribute is created as a class attribute, not an instance attribute. 
Example:
 class Widget:
 # create "color" as class attribute, not within __init__()
 color = <<property-object>>
 def __init__(self, ...):
 # do not define "self.color" instance attribute
The property object can be created with the built-in function 
property(), which in some cases can be coded as a decorator: @property. 
The property object can also be an instance of a class that implements 
the descriptor protocol.
---------------------------------------------
Tx,
John
[1] http://docs.python.org/glossary.html


More information about the Python-list mailing list

AltStyle によって変換されたページ (->オリジナル) /