New-Style classes in Python
Actually not really new (introduced in 2.2)
Concept
Descriptors (descriptor protocol)::
- Properties, bound/unbound method call, static / class method are all based on the 'descriptor protocol'
Descriptor
Definition:
- object attribute with 'binding behavior'
Classification:
Data descriptor |
(with get and set) |
[ for properties ] |
Non-data descriptor |
(with get only) |
[ for method, or likes ] |
Example:
print boy.name
Flow:
The simplest case is boy.__dict__['name'] type(boy).__dict__['name'] type(boy).__base__.__dict__['name'] If, a valid entry is looked up in the dictionary. and if the entry is a object with '__get__', '__set__', '__delete__' then it is a 'data-desc' the descriptor protocol will be used.
Each 'Object' have "attributes"
- data properties
- methods
Notes for 'overriding MAGIC method'
descriptor is always triggered by getattribute()
so, if you override getattribute, you need to to implement the descriptor protocol handling, otherwise, descriptor won't work anymore
Ref: How-To Guide for Descriptor: