Python Class (vi)-static methods, class methods, property methods

Source: Internet
Author: User

    • Static methods

The static method is defined by @staticmethod, but the properties in the class and instance are not accessible in the static method, but the static method requires the class to invoke the

#-*-Coding:utf-8-*-__author__ = "MuT6 Sch01ar" class Person (object):    def __init__ (self,name):        self.name = name    @staticmethod    def Eat (Self,food):        print ("%s is eating%s"% (Self.name,food)) if __name__ = = ' __main__ ':    p = person (' John ')    p.eat (' meat ')

Run, error

The parameters of the Eat method are removed and printed directly and can be called directly

#-*-Coding:utf-8-*-__author__ = "MuT6 Sch01ar" class Person (object):    def __init__ (self,name):        self.name = name    @staticmethod    def Eat ():        print ("John is eating") if __name__ = = ' __main__ ':    p = person (' john ')    P.eat ()

Run results

If you want to pass parameters to eat (), you can pass the instantiated person into

#-*-Coding:utf-8-*-__author__ = "MuT6 Sch01ar" class Person (object):    def __init__ (self,name):        self.name = name    @staticmethod    def Eat (self):        print ("%s was eating"%self.name) if __name__ = = ' __main__ ':    p = person ( ' John ')    p.eat (p)

Run results

    • Class method

Class methods are defined by @classmethod.

Class methods can access only class variables and cannot access instance variables

#-*-Coding:utf-8-*-__author__ = "MuT6 Sch01ar" class Person (object):    name = ' Jack '    def __init__ (self,name): 
   self.name = name    @classmethod    def Eat (self):        print ("%s is eating"%self.name) if __name__ = = ' __main__ ':    p = person (' John ')    p.eat ()

Run results

The instance variable was passed to John, but Jack was the one who printed it.

Because class methods cannot access instance variables, class methods access class variables in the class

    • Property method

Defining attribute methods with @property

To change a method in a class to a static property

#-*-Coding:utf-8-*-__author__ = "MuT6 Sch01ar" class Person (object):    def __init__ (self,name):        self.name = Name    @property    def Eat (self):        print ("%s was eating"%self.name) if __name__ = = ' __main__ ':    p = person (' John ')    P.eat

Invoke the property method by invoking the property's method

If you want to pass arguments to attribute methods, use setter

Python Class (vi)-static method, class method, 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.