Differences between Python class methods, static methods, and instance methods

Source: Internet
Author: User

Differences between Python class methods, static methods, and instance methods
The Python method does not require C #. Java compilation languages strictly distinguish static and instance methods. That is to say, the Python static method, the class method and the instance method are only different in the call, type and instance can call. The general rules are as follows:
A: instance method: The method without @ classmethod and @ staticmethod is the instance method. Assume that there are n Parameter comparison parameters, and n parameters need to be passed for the type call. When an instance is called, only n-1 parameters can be passed, because the first parameter is passed by the system by default. Because of this rule, if the instance method has no parameters, the instance cannot be called, but the type can be called.
B: Class Method: also called the type method. Use @ classmethod to mark the method. assume that there are n required parameters. If it is a class call, n-1 parameters need to be passed. The first parameter is also a set type. If an instance is called, n parameters are required. The class method must have at least one parameter to call the type.
C: static method: The method marked by @ staticmethod. Class and instance can be called. There is no fixed parameter problem, and the parameters must be passed as many parameters as the method has.
Of course, the parameter here ignores the default parameter. This is not a big problem to understand. The following is a sample code. You can call the test. Python does not overload (overload, not override .??

Class ObjOpt:
Def _ init _ (self ):
Self. Name = 'abc'
Self. CCC = 123
# Instance method
Def ClassMethodFx3 (ad, abc ):
Return dir (ad)
# Instance methods, instances and classes can be called, but n parameters must be passed for class calls,
# Only n-1 parameters can be passed for instance calls. The first parameter is the instance itself by default.
Def ClassMethodFx1 (cls, abc ):
Return dir (cls)

# Class methods, instances and classes can be called. The first parameter is of the type by default.
# Ignore the first parameter when calling. You only need to pass n-1 parameters.
# Cls is not a keyword
@ Classmethod
Def ClassMethodFx21 (cls, abc ):
Return dir (cls)

# Class methods, instances and classes can be called. The first parameter is of the type by default.
# Ignore the first parameter when calling. You only need to pass n-1 parameters.
@ Classmethod
Def ClassMethodFx22 (bbb, abc ):
Return dir (bbb)
# Static methods can be called by instances and classes, but no default parameters are provided. n parameters need to be passed.
@ Staticmethod
Def GetStaticMethod (cls, obj ):
Return dir (cls)
# Instance method
Def GetProperties (self, obj ):
TheAttrs = dir (obj)
TheRetAttrs = []
For attr in theAttrs:
Bb = attr. startswith ('__')
If bb = False:
TheRetAttrs. append (attr)
Return theRetAttrs;
Name = 'abcd'

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.