Static properties, class methods, static methods for Python classes

Source: Internet
Author: User

1, static properties. @property. The function is to encapsulate the functions of a class into similar data attributes.

class Student (object):     School='szu'    @property    def printmassage (self):         Print ('aaaa') S1=Student () s1.printmassage    #aaaa

2, the class method: Is the class object has the method, needs to use the decorator @classmethod to identify it as the class method, for the class method, the first parameter must be the class object, generally takes cls as the first parameter, can through the instance object and the class object accesses.

Class methods generally have two functions: one is to access the class properties. The second is to modify the class properties

class Student (object):    School='szu'    @classmethod    def  Printmassage (CLS):        print(cls.school)    S1=Student () Student.printmassage ()  #szus1.printmassage ()       #Szu

3, static method: Static method is actually independent, depending on the class, but the actual is only to adjust the relationship. Just nominal collation management, is actually a toolkit that can be called by classes and instances. A static method cannot call a class method directly, and a call must be called with the class name.

classStudent (object): School='Szu'@staticmethoddefprintmassage ():Print(student.school) @staticmethoddeffunc (x, y):Print(x, y) s1=Student () student.printmassage ( )#SzuS1.printmassage ()#SzuStudent.func ()#1 2S1.func (ON)# the

Static properties, class methods, static methods for Python classes

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.