Python property methods, static methods, class methods

Source: Internet
Author: User

Methods in the class add the following adorner

Property Method: @property Change the method to a static property of the class, without parentheses when calling. To hide the implementation of the details, for the user, the feeling is to find a dead thing to write

classFlight (object):def __init__(self,name): Self.name=namedefcheck_flight (self):Print('Checking Flight%s:status'%self.name) Status= 3returnStatus @property#as a static property of a class, it is called without parentheses. To hide the implementation of the details, for the user, the feeling is to find a write dead static properties    defFlight_status (self): status=self.check_flight ()ifStatus = =0:Print('flight got canceled ...')        elifStatus = = 1:            Print('flight is arrived ...')        elifStatus = = 2:            Print('flight has departured already ...')        Else:            Print('cannot confirm the flight status .... Please check later') @flight_status. Setter#Modify the parameters inside the property method    defFlight_status (self,status): Status_dict={0:'canceled',            1:'arrived',            2:'departured'        }        Print('\033[31;1mhas changed flight:%s status to:%s\033[0m'%(Self.name,status_dict[status])) @flight_status. Deleter#This static method can be deleted by adding the adorner.    defFlight_status (self):Print('status Func got removed') F1= Flight ("Chuan Hang 3u8633") F1.flight_status#no parenthesesF1.flight_status = 1F1.flight_statusdelF1.flight_statusf1.flight_status
View Code

static method: @staticmethod

Only nominal collation management, in fact, in the static method can not access any property of the class or instance

classDog (object):def __init__(self,name): Self.name=namedefTalk (self):Print('%s talking: Chewy! '%self.name) @staticmethoddefEat (Self,food):Print('%s Eating%s'%(Self.name,food)) D1= Dog ('Chenronghua') D1.eat ('steamed Bun')
View Code
Traceback (most recent):  File "c:/users/administrator/desktop/python3_study/day6/static Method _ class method. py", line 15, In <module>    d1.eat (' Bun ') typeerror:eat () Missing 1 required positional argument: ' Food '

Class method: @classmethod

Only class variables can be accessed and instance variables cannot be accessed

classDog (object): Name='Class method call Test with class variable'    def __init__(self,name): Self.name=namedefTalk (self):Print('%s talking: Chewy! '%self.name)#@staticmethod@classmethoddefEat (Self,food):Print('%s Eating%s'%(Self.name,food)) D1= Dog ('Chenronghua') D1.eat ('steamed Bun')
View Code
class method call test class variable eating bun process finished with exit code 0

  

Python property methods, static methods, class 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.