Python development "seventh chapter": Python Object-oriented advanced

Source: Internet
Author: User

Static Methods

  The method of decorating can be changed into a static method through the @staticmethod decorator, what is static method? In fact, it is not difficult to understand, the ordinary method can be called directly after the instantiation, and in the method can pass self. Invokes an instance variable or class variable, but a static method cannot access an instance variable or a class variable . A method that cannot access instance variables and class variables is, in fact, unrelated to the class itself, and its only association with a class is the need to call this method through the class name

#静态方法class schoolmate (object):    def __init__ (self, name):        self.name = name    @staticmethod    # Turn the Eat method into a static method    Def eat (self):        print ("%s is eating"% self.name) p= schoolmate ("Lianzhilei") P.eat () # Traceback ( Most recent Call last): #   File ' c:/users/l/pycharmprojects/s14/class/day7/staticmothod.py ', line +, in <module >#     p.eat () #TypeError: Eat () Missing 1 required positional argument: ' Self '

The above call will have the following error, saying that eat needs a self parameter, but the call is not passed, yes, when the eat becomes a static method, and then through the instance call will not automatically pass the instance itself as a parameter passed to the auto .

There are two ways to make the above code work correctly

    • When invoked, the instance itself is passed to the Eat method, i.e. P.eat (p)
    • The self parameter is removed from the Eat method, but it also means that you cannot pass self in eat. Calls to other variables in the instance
#静态方法class schoolmate (object):    def __init__ (self, name):        self.name = name    @staticmethod    # Change the Eat method to a static method    Def eat (self):        print ("%s is eating"% self.name) p= schoolmate ("Lianzhilei") P.eat (P) # Lianzhilei is eating# static method class Schoolmate (object):    def __init__ (self, name):        self.name = name    @ Staticmethod    # Turn the Eat method into a static method    Def eat ():        print ("is eating") p= schoolmate ("Lianzhilei") p.eat () #is Eating

  

Class method

Class methods are related to classes by definition, class methods are implemented by @classmethod adorners, and the difference between class methods and ordinary methods is that class methods can only access class variables and cannot access instance variables

#类方法class schoolmate (object):    def __init__ (self, name):        self.name = name    @classmethod   # Turn the Eat method into a class method    def Eat (self):        print ('%s is eating '% self.name) p= schoolmate ("Lianzhilei") P.eat () # Traceback (most recent call Last): #   File ' c:/users/l/pycharmprojects/s14/class/day7/classmothod.py ', line <module>#     p.eat ( ) #   File "c:/users/l/pycharmprojects/s14/class/day7/classmothod.py", line eat#     print ("%s is eating"% Self.name) # Attributeerror:type object ' schoolmate ' has no attribute ' name '

The execution error is as follows, saying that Schoolmat does not have a name attribute, because name is an instance variable , the class method cannot access the instance variable , only the class variable can be accessed

#类方法class schoolmate (object):    name = ("Schoolmat class variable")    def __init__ (self, name):        self.name = name    @ Classmethod   # Turn Eat method into class method    Def eat (self):        print ("%s is eating"% self.name) p= schoolmate ("Lianzhilei") P.eat () # Schoolmat class variable is eating

You can define a class variable at this time, and the variable name is name to resolve

Property method

  The function of the property method is to turn a method into a static property through @property, which is quite useful, the later course will involve, the code first

Python development "seventh chapter": Python Object-oriented advanced

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.