Python property, Properties
As the name implies, property is used to generate an attribute. Operations on this attribute can be mapped to operations on certain functions, similar to C #.
Format:
Pvar = propery (get_func, set_func, del_fun, doc_func );
Call the corresponding functions at get, set, del, and doc respectively;
Try to define this
>>> Aa = property (lambda: hello, lambda x: print (x ))
>>> Aa
>>> Aa = 1
It seems thatProperty only makes sense in class (read later)
>>> Class Test:
... Aa = property (lambdaSelf: Hello, lambdaSelf, X: print (x ))
...
>>> T = Test ()
>>> T. aa
'Hello'
>>> T. aa = (1, 2)
(1, 2, 2)
The secret of property
In fact, propery is not a real function, but a class.
>>> Type (property)
The common function is
>>> Def f ():
... Pass
...
>>> Type (f)
The property class implements_ Get __, _ set __, _ delete __Methods. When these three methods are combined, the descriptor rules are defined and the object that implements any of the methods is calledDescriptor (descriptor). The special feature of the descriptor is how it is accessed. For example, when a program reads an attribute, if the attribute is bound to an object that implements the _ get _ method, then the _ get _ method will be called to return the result, instead of simply returning the object;
ClassSomething:
... Def _ get _ (self, instance, owner ):
... Print (instance)
... Print (owner)
... Return _ get __
... Def _ set _ (self, instance, name ):
... Print (instance)
... Print (name)
... Print (_ set __)
>>> Class Holder:
...S = Something ()
...
>>> H = Holder ()
>>> H.s
<__ Main _. Holder object at 0x7fdcd9ed8518>
'_ Get __'
>>> H.s = 3
<__ Main _. Holder object at 0x7fdcd9ed8518>
3
_ Set __
The parameter instances of the _ get _ and _ set _ methods of Something correspond to the objects bound to the Something object. As you can imagine,Property is the get, set, del, doc;
Note that:The descriptor must be assigned to a class attribute instead of an object instance attribute.;
>>> Class Something:
... Pass
>>> Setattr (Something, 'A ',Property (lambda self: hello, lambda self, x: print (x )))
>>> T. aa
'Hello'
>>> T. aa = 3
3
The setting on the object instance has no effect:
>>> Setattr (t, 'A', property (lambda self: hello, lambda self, x: print (x )))
>>> Setattr (Something, 'A', None)
>>> T. aa
>>> T. aa = 3
You can alsoAnnotation(Annotation) method to define property
Class C (object ):
| @ Property
| Def x (self ):
| I am the 'X' property.
| Return self. _ x
| @ X. setter
| Def x (self, value ):
| Self. _ x = value
| @ X. deleter
| Def x (self ):
| Del self. _ x
Http://www.ibm.com/javasworks/library/ospythondescriptors/provides a method for creating propertyin a dynamic state.
Class Person (object ): DefAddProperty(Self, attribute ): # Create local setter and getter with a special attribute name Getter = lambda self: self. _ getProperty (attribute) Setter = lambda self, value: self. _ setProperty (attribute, value) # Construct property attribute and add it to the class Setattr(Self._ Class __, Attribute, property (fget = getter, Fset = setter, Doc = Auto-generated method )) Def _ setProperty (self, attribute, value ): Print Setting: % s = % s % (attribute, value) Setattr (self, '_' + attribute, value. title ()) Def _ getProperty (self, attribute ): Print Getting: % s % attribute Return getattr (self, '_' + attribute) |