Python3 Property Properties

Source: Internet
Author: User

The property in Python3 has a very interesting function that can invoke the methods in the class like class properties!

Class Property (Fget=none, Fset=none, Fdel=none, Doc=none)

Let's take a quick look at this property class, here's an example from the official website:

class c:    def __init__ (self):         self._x = none    def getx (self):         return self._x    def setx (Self,  value):        self._x = value     Def delx (self):         del self._x    x  = property (getx, setx, delx,  "I ' m the  ' x '  property.")   #  here x equals the class attribute     c = c ()   #  generates an object c.x =  10  #  set self._x=10, the actual call is the class Setx method c.x  #  get the value of self._x, actually called the Getx method in the Class Del  c.x  #  Delete the value of self._x, the actual call is the Delx method in the class 

It feels very interesting, it is incredible! Property Fget is a function that gets the value of an attribute; Fset is a function that sets a property value; Fdel is a function that deletes a property value; Doc creates a docstring for the property. You can easily use the corresponding function that you write at the corresponding parameter position when you use it.


We can also use the property as an adorner, or use the official website example:

Class C:def __init__ (self): self._x = None @property def x (self): "" "I ' m the ' X ' property." " return self._x @x.setter def x (self, value): self._x = value @x.deleter def x (self): del sel F._x

Property object has getter, setter, deleter three methods, getter Get property value, setter Set property value, Deleter set attribute value, this example effect is exactly the same as the previous example! We see that the method name inside is identical, but the effect is different. The first X method is to get the property value, the second X method is to set the property value, and the third X method is to delete the property value.

Do you see here that this is all a property to help you do, wrong, wrong, wrong! In fact, the property has only done an event, it will be your method like a class attribute, as for the inside of the search, delete, change, in fact, you write the function of the implementation! Fget, Fset, Fdel, setter, deleter These are just names and, conveniently identifiable, nothing else!

Let's look at the source code of the property:

Class property (object):     "" "    property (fget=None, fset= None, fdel=none, doc=none)  -> property attribute         fget is a function to be used for getting an  attribute value, and likewise    fset is a function  for setting, and fdel a function for del ' ing, an     attribute.  Typical use is to define a managed  Attribute x:        class c (object):         def getx (self): return self._x         def setx (Self, value): self._x = value         def delx (self): &NBSp;del self._x        x = property (Getx, setx,  delx,  "I ' m the  ' x '  property.")         decorators make defining new properties  or modifying existing ones easy:         Class c (object):         @property          def x (self):             "I  am the  ' x '  property. "             return self._x         @x.setter        def x (self,  Value):            self._x = value        &nBsp;@x.deleter        def x (self):             del self._x     "" "     Def deleter (Self, *args, **kwargs): # real signature unknown          "" " descriptor to change the deleter on  a property.  "" "        pass    def  getter (Self, *args, **kwargs): # real signature unknown          "" " descriptor to change the getter on a  property.  "" "        pass    def  Setter (Self, *args, **kwargs): # real signature unknown          "" " descriptor to change the setter on a property.  "" "         PASS    DEF __DELETE__ (self, *args, ** Kwargs): # real signature unknown         "" "  delete an attribute of instance.  "" "         pass    def __getattribute__ (Self, *args, **kwargs): # real  signature unknown         "" " return getattr (self,  Name) .  "" "        pass    def __get__ ( Self, *args, **kwargs): # real signature unknown          "" " return an attribute of instance, which is of  type owner.&nbsp, "" "         pass    def __init__ (self, fget= None, fset=none, fdel=none, doc=none): # known special case of  property.__init__         "" "         property (Fget=none, fset=none, fdel=none, doc=none)  -> property  Attribute                fget  is a function to be used for getting an attribute  Value, and likewise        fset is a function  for setting, and fdel a function for del ' ing, an         attribute.  typical use is to define a  managed attribute x:                 class c (object):            def  Getx (self): return self._x             Def setx (Self, value): self._x = value             def delx (self): del self._x             x = property (getx, setx, delx,  "I ' m the   ' x '  property. ")                 decorators  make defining new properties or modifying existing ones easy:                 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                #  ( Copied from class doc)          "" "         pass     @staticmethod  # known case of  __new__    def __new__ (*args, **kwargs):  # real signature  unknown         "" " Create and return a  New object.  see help (type)  for accurate signature.  "" "         pass    def __set__ (self, *args, ** Kwargs): # real signature unknown         "" "  set an attribute of instance to value.  "" "         pass &Nbsp;  fdel = property (Lambda self: object (),  lambda self, v:  none, lambda self: none)   # default    fget =  property (Lambda self: object (), lambda self, v: none, lambda  Self: none)   # default    fset = property (lambda self:  object (),  lambda self, v: none, lambda self: none)   #  Default    __isabstractmethod__ = property (Lambda self: object (),  lambda self, v: none, lambda self: none)   # default

See the above source code suddenly no, Fdel, Fget, fset all just execute the code inside your function! So let's just remember a word: "property allows your method to be used like a class attribute."



This article is from the "Daibayang blog" blog, make sure to keep this source http://daibaiyang119.blog.51cto.com/3145591/1972460

Python3 Property Properties

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.