Python Learning Path: Staticmethod Classmethod Property method

Source: Internet
Author: User

Reference Link: http://www.cnblogs.com/alex3714/articles/5213184.html

Static methods

Only nominal collation management, in fact, no property in the class or instance can be accessed in a static method

Class Dog (object):    def __init__ (self,name):        self.name = name    @staticmethod #实际上跟类没什么关系了    def eat (self ):        print ("%s is eating%s"% (Self.name, ' DD '))    def talk (self):        print ("%s is talking"%self.name) d = Dog (" Chenrh ") d.eat (d) d.talk ()

class Method:

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

Class Dog (object):    #n = 3333    name = "Huazai"    def __init__ (self,name):        self.name = name    #@ Staticmethod #实际上跟类没什么关系了    @classmethod    def Eat (self):        print ("%s is eating%s"% (Self.name, ' DD ')    def Talk (self):        print ("%s is talking"%self.name) d = Dog ("Chenrh") D.eat () D.talk ()

Property Method:

Turn a method into a static property

Class Dog (object):    #n = 3333    name = "Huazai"    def __init__ (self,name):        self.name = name    #@ Staticmethod #实际上跟类没什么关系了    # @classmethod    @property #attribute    def Eat (self):        print ('%s is eating%s ' % (Self.name, ' DD '))    # def talk (self):    #     Print ("%s is talking"%self.name) d = Dog ("Chenrh") d.eat

Class Dog (object):    #n = 3333    name = "Huazai"    def __init__ (self,name):        self.name = name        Self.__food =none    # @staticmethod # #实际上跟类没什么关系了    # @classmethod    @property #attribute, turn a method into a property    def eat (self):        Print ("%s is eating%s"% (Self.name,self.__food))    @eat. setter# Modify Properties    def eat (Self,food):        print ("Set to food:", food)        Self.__food = Food    @eat. deleter #删除私有属性    def Eat (self):        del self.__food        print ("erased")    # def talk (self):    #     print ('%s ' is Talking "%self.name) d = Dog (" Chenrh ") d.eatd.eat =" Baozi "del d.eat

Examples of scenarios where static property methods apply:

You want to know the current status of a flight, is arrived, delayed, canceled, or has flown away, want to know this state you have to go through the following steps:

1. Connecting Airlines API Query

2. Parsing the query results

3. Return the results to your users

So the value of this status property is the result of a series of actions, so every time you call, it is actually going through a series of actions to return your results, but these action procedures do not require user care, the user only need to call this property can, understand?

Class Flight (object): Def __init__ (self,name): self.flight_name = name def checking_status (self): Prin  T ("Checking flight%s status"% Self.flight_name) return 1 @property def flight_status (self): status            = Self.checking_status () if status = = 0:print ("Flight got canceled ...") elif status = = 1:        Print ("Flight is arrived ...") elif status = = 2:print ("Flight has departured already ...") Else:print ("Cannot confirm the flight status...,please check later") @flight_status. Setter #修改 def Flig Ht_status (self,status): Status_dic = {0: "Canceled", 1: "Arrived", 2: "Departur Ed "} print (" \033[31;1mhas changed the flight status to \033[0m ", Status_dic.get (status)) @flight_status. Deleter #删除 def flight_status (self): print ("Status got removed ...") F = Flight ("CA980") f.flight_statusf.flight_s Tatus = 2 #触发 @flight_stAtus.setterdel f.flight_status #触发 @flight_status. deleter  

Python Learning Path: Staticmethod Classmethod Property method

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.