Python path-attribute methods, class methods, static methods

Source: Internet
Author: User

Chapter Content

    1. Property method
    2. Class method
    3. Static methods
Property Method

The property method is to change @property a method into a static property by using an adorner, so that we can access the property, or get the return value of a method

1  fromUrllib.requestImportUrlopen2 classWeb_page:3     def __init__(self, url):4Self.url =URL5Self.__content=None6     #turning the content method into a property7 @property8     defcontent (self):9         #Return private PropertyTen         returnSelf.__content ifSelf.__content ElseUrlopen (Self.url). Read () Onecon = web_page ('www.baidu.com') Ares =con.content - Print(RES)

Three methods have been implemented for us in the property, get, set, delete

1 classFoo:2     #Get Properties3 @property4     defAAA (self):5         Print("a Get method was executed")6     #Setting property values7 @AAA. Setter8     defAAA (self, value):9         Print("The Set method is executed")Ten     #Delete Property One @AAA. Deleter A     defAAA (self): -         Print("The Delete method was executed") - #instantiation of thef =Foo () - #Get Properties - f.aaa - #set the property value, you must set the parameter, even if you do not use +F.AAA ='AAA' - #Delete attribute values + delf.aaa A " " at Execution Result: - a Get method was executed - The Set method is executed - The Delete method was executed - " "

Look at it in a different way.

1 classFoo:2     defget_aaa (self):3         Print('a Get method was executed')4     defset_aaa (self,value):5         Print('The Set method is executed')6     defdelete_aaa (self):7         Print('The Delete method was executed')8     #Instantiate Property class9AAA =Property (Get_aaa, SET_AAA, DELETE_AAA)Ten #instantiation of Onef =Foo () A #gets the property directly called and executes the GET_AAA - f.aaa - #set the property value, the incoming parameter executes the SET_AAA theF.AAA ='AAA' - #Delete attribute value, executed delete_aaa - delf.aaa - " " + Execution Result: - a Get method was executed + The Set method is executed A The Delete method was executed at " "

Practical application

1 classGoods:2     def __init__(self):3         #Original Price4Self.original_price = 1005         #Discount6Self.discount = 0.87 @property8     defPrice (self):9         #actual rate = Original Price * DiscountTenNew_price = Self.original_price *Self.discount One         returnNew_price A @price. Setter -     defPrice (self, value): -Self.original_price =value the @price. Deleter -     defPrice (self): -         delSelf.original_price -Goods =Goods () +Goods.price#Get discounted product prices -Goods.price = 200#revise the original price of the product + Print(Goods.price) A delGoods.price#Delete Item Price
class Method

A class method is a @classmethod adorner that converts a normal method into a class method, a class method can only interact with a class property, cannot access an instance variable, and a default CLS parameter is passed in to represent this class

1 classPerson :2Country =' China'3     def __init__(self,name,age):4Self.name =name5Self.age = Age6 @classmethod7     defSearch (CLS):8         #An instance variable cannot be used in a class method and throws a Attributeerror9         Print("I come from {}". Format (cls.country))Ten         #print ("{} come from {}". Format (self.name,cls.country)) Error Onep = Person ('Lyon',' -') A P.search () - #execution Result: I come from China

PS: The default parameter in the class method can be changed to self, and will not change the result, also can only access the class variable, cannot access the instance variable

Static Methods

The static method is to turn a method in a class into a static method through the @staticmethod adorner

Static methods, like static properties, can pass self in a class. , but static is not able to access instance variables or class variables, which means that the self in the static method has nothing to do with this class, and that the only association with this class is the need to call through the class name

1 classPerson :2Country =' China'3     def __init__(self,name,age):4Self.name =name5Self.age = Age6     #is not much of a relationship with this class, so the properties in the class cannot be called7 @staticmethod8     defsearch ():9         Print("I'm a static method .")Tenp = Person ('Lyon',' -') One P.search () A #execution Result: I am a static method

With self, self is just a normal argument.

1 classPerson :2Country =' China'3     def __init__(self,name,age):4Self.name =name5Self.age = Age6 @staticmethod7     defSearch (self):8         Print("{} come from {}". Format (self.name,self.country))9p = Person ('Lyon',' -')Ten #passing an instance into the search method One P.search (P) A #execution Result: Lyon come from China

Python path-attribute methods, class methods, static methods

Related Article

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.