On the general method, static method (Staticmethod) and class method (Classmethod) in Python

Source: Internet
Author: User

Let's start with a brief discussion of the differences between general methods, static methods, and class methods in Python classes.

1. General Methods in classes

The general method, when defined, requires a parameter that represents the class instance (usually self, such as Def foo (self,arg1,arg2 ...). ), the generic method cannot be called by the class name. Method Name (), you must first create an instance of the class, and then invoke it through the instance. Method Name () .

2. Static methods in classes

Static methods in a class are decorated with "@staticmethod", which, when defined, does not need to represent an instance of the class, but can be defined as a method outside the class. Static methods can be called by the class name. Method Name () and instance. Method Name () .

3. Class methods in classes

Class methods are decorated with "@classmethod", and when defined, the class method needs to have parameters that represent the class object. Class methods can also be called by the class name. Method Name () and instance. Method Name () . (Note: In Python, a class is also an object, and the class object referred to here refers to the class itself, not to the class instantiation of the object)


Let's take a look at the actual example:

First, define a class:

Class TestMethod (object): #一般方法, static methods, and class methods are independent of the new class, we use the new class for example, but note that the following analysis also applies to the classic Class Def Test (self): #这里的self参数是必须的, which       Represents an instance of a class, but is not necessarily represented by the string "self" and can be represented by any character, except that we typically use self to represent print ("Object") @classmethod def test2 (CLSS): #这里的clss参数意思必须的, which represents a class object, usually the formal parameter in which we "CLS" Represents print ("class") @staticmethod def test3 (): #这 Can not need any formal parameter print ("Static")

First we use the class name. Method name () to invoke:

The results are as follows:

>>> testmethod.test () Traceback (most recent): File "<stdin>", line 1, in <module>typeerror : Unbound method Test () must is called with TestMethod instance as first argument (got nothing instead) >>> Testme Thod.test2 () class>>> testmethod.test3 () static

As you can see, the generic Method test () calls the exception and must be called through the instance


We then use the instance invocation:

>>> t = TestMethod () >>> t.test () object>>> t.test2 () class>>> t.test3 () static


On the general method, static method (Staticmethod) and class method (Classmethod) in Python

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.