Property built-in adorner functions and @name.setter, @name. deleter

Source: Internet
Author: User

# Property    #built-in adorner functions are used only in object-oriented    #post-decoration effect: Disguise the method of a class as a property    #The method after being decorated by the property cannot take any parameters except self fromMathImportPiclassCircle:def __init__(self, R): SELF.R=Rdefperimeter (self):return2 * pi *SELF.RdefArea (self):returnPI * self.r**2 *Pic1= Circle (5)#The above example is to find the area and perimeter of a circle, however the circumference and area of the circle should actually be a property of the circle, but the above circle class has no area and perimeter properties#so we can only call the element's area method and get the area value.Print(C1.area ())#but we can decorate the class method with the property decorator, so that when the class method is called, it looks like it is calling the attribute directly to get the value, and the method does not add () when calling the methodclassCircle:def __init__(self, R): SELF.R=R @propertydefperimeter (self):return2 * pi *SELF.R @propertydefArea (self):returnPI * self.r**2 *Pic1= Circle (5)Print(C1.area)#246.74011002723395 like calling properties directlyPrint(C1.perimeter)#31.41592653589793 like calling properties directly# ------------------------------------------------------------------classpersion:def __init__(self, name): Self.__name=name @propertydefname (self):returnSelf.__name+'SB'Tiger= Persion ('haha')Print(Tiger.name)#tiger.name = ' Dede '#in the example above, we have disguised the name method as a property, so we can invoke this method directly as if it were an Access property .#However, the name is disguised as a property, but can not tiger.name = ' dede ' to modify the name, it seems to disguise the feeling of the property#also close, because the public property can be modified, so use the following method can be modified by the method to modify the operationclasspersion:def __init__(self, name): Self.__name=name @propertydefname (self):returnSelf.__name+'SB'@name. Setter#Name is the name of the property decoration, the purpose is to let name as an lvalue, after name.setter, you can define a method to modify the name, can only receive a parameter, used as the right value of the equal sign    defname (self, newName): Self.__name=Newnametiger= Persion ('haha')Print(tiger.name) tiger.name='Dede'Print(Tiger.name)#Property Application example, supermarket fruit suddenly began to call 50 percentclassGoods:discount= 0.5def __init__(self, Name, price): Self.name=name self.__price=Price @propertydefPrice (self):returnSelf.__price*Goods.discountapple= Goods ('Apple', 5)Print(Apple.price)#2.5# --------------------------------------------------------------------#Delete the properties of a class and modify the properties of a classclasspersion:def __init__(self, name): Self.__name=name @propertydefname (self):returnSelf.__name@name. Deleter#after decorating with this thing, the decorated method will be called after del P.name.    defname (self):delSelf.__name #Delete Property@name. Setter#Name.setter, when name is used as the left value, the method to be decorated is called, and the right value is passed as a parameter to the NewName    defname (self, newName): Self.__name=NEWNAMEP= Persion ('wer')Print(P.name)#werdelP.name#The __name attribute in persion has been removed#print (p.name)#Classmethod#Staticmethod

Property built-in adorner functions and @name.setter, @name. deleter

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.