Classmethod, Staticclassmethod built-in adorner function

Source: Internet
Author: User

#method English is the meaning of methods#Classmethod class Method    #when a method in a class involves only the static properties of an operation class, logically we want to call this method directly through the class name to modify the static properties of the class, which can be used with this built-in adorner function#Staticmethod static Method#method of Class ClassmethodclassGoods:discount= 0.5#Discount    def __init__(self, Name, price): Self.name=name self.__price=Price @propertydefPrice (self):returnSelf.__price*Goods.discountdefChange_discount (self, Newdiscount):#Modify DiscountGoods.discount =Newdiscountapple= Goods ('Apple', 5)Print(Apple.price)#2.5Apple.change_discount (2)#This can modify the discount, but the concept is not understandable, because the discount is modified by the whole supermarket discount, but here actually using Apple method to modify the class discount, is unreasonablePrint(Goods.discount)classGoods:discount= 0.5#Discount    def __init__(self, Name, price): Self.name=name self.__price=Price @propertydefPrice (self):returnSelf.__price*Goods.discount @classmethod#methods that are modified by this adorner can also be called directly by the class name to the CLS Representative class, like self (representing the object)    defChange_discount (CLS, newdiscount):#Modify DiscountCls.discount =Newdiscountapple= Goods ('Apple', 5)Print(Apple.price) Goods.change_discount (0.2)#you can then invoke the method directly from the class name to modify the discount for the entire class.Print(Apple.price)#1.0#static method of the Staticmethod class    #In fully object-oriented programming, if a method has nothing to do with the object and is not related to the class, use Staticmethod to turn the method into a static methodclassLogin:def __init__(self, Name, password): Self.name=name Self.pwd=PassworddefLogin (self):Pass@staticmethod#the decorated method becomes a static method, which is not related to the object, and is not related to the class, n    defget_usr_pwd (): USR= Input ('User name') PWD= Input ('Password') Login (usr, pwd) login.get_usr_pwd ()#in fully object-oriented programming, if a method has nothing to do with the object and has no relation to the class, then use Staticmethod to turn this method into a static method that can be called directly#class methods and static methods are called by the class.#objects can also invoke class methods and static methods, but they should not be used

Classmethod, Staticclassmethod built-in adorner function

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.