Python classmethod Class Method

Source: Internet
Author: User
Tags python classmethod

There are three main points of the python classmethod class method:

1 in Python, The. Class Method @ classmethod is a function modifier, which indicates that the next class method is used. What we usually see is the instance method. The first parameter CLS of the class method, and the first parameter of the instance method is self, which indicates an instance of the class.
2. A common object method requires at least one self parameter, representing a class object instance.
3
The class method has the class variable CLS passed in, so that you can use CLs for some related processing. In addition, when a subclass is inherited and this class method is called, The passed-in class variable CLS is a subclass rather than a parent class.
Class methods can be called through classes, just like C. F (), which is a bit similar to static methods in C ++,
You can also call it through an instance of the class, just like C (). F (), Here C (). After writing it like this, it is an instance of the class.

# Encoding = UTF-8

Class Info(Object):

@ Classmethod
Def Sayclassmethod(CLS):

Print 'Say% S' % CLS

Def Saymethod(Self):

Print 'Say% S' % Self

Test = Info()
Test.Saymethod()# Instance call Method

Test.Sayclassmethod()# Instance call Methods

Info.Saymethod(Test)# Class call instance method

Info.Sayclassmethod()# Class call MethodsLet's compare and run it.

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.