The Self,cls,staticmethod,classmethod of Python learning

Source: Internet
Author: User

I. GENERAL description

These three words appear in the Python class, and the self and the CLS can be replaced by other words, and there are three methods of the class.

One is defined by Def, the ordinary general, need to pass at least one parameter, generally with self, such a method must be accessed through an instance of a class, similar to C + + through the object to access;

The second is to add @classmethod in front of Def, this kind of method is a feature that can be called by the class name, but also must pass a parameter, generally with the CLS to represent class, that can be called directly through the class;

The third is to add @staticmethod in front of Def, this class method is static class method, similar to C + + static function, one of his characteristics is that the parameter can be empty, also support the class name and object two kinds of call mode;

A normal method, the first parameter needs to be self, which represents a specific instance of itself.
If you use Staticmethod, you can ignore this self and use this method as a normal function.
For Classmethod, its first argument is not self, which is the CLS, which represents the class itself.
>>> class A (object):
def foo1 (self):
Print "Hello", self
@staticmethod
Def foo2 ():
print "Hello"
@classmethod
def Foo3 (CLS):
Print "Hello", CLS
>>> a = A ()
>>> a.foo1 () #最常见的调用方式, but the same way as below
Hello <__main__. A Object at 0x9f6abec>
>>> a.foo1 (A) #这里传入实例a, equivalent to the normal method of self
Hello <__main__. A Object at 0x9f6abec>
>>> A.foo2 () #这里, because there are no parameters to the static method, it is possible to pass things
Hello
>>> A.foo3 () #这里, because it is a class method, its first argument is the class itself.
Hello <class ' __main__. A ' >
>>> a #可以看到, enter a directly, and return the same information as the above call.
<class ' __main__. A ' >

Second, in-depth understanding

Actually SELF,CLS is not a keyword, for example

class A:      member  = "this is a test."      def __init__( self ):          pass
     @ classmethod      def Print1( cls ):          print "print 1: " cls .membe        def Print2( self ):          print "print 2: " self .member                      @ classmethod      def Print3(paraTest):          print "print 3: " , paraTest.member = A() A.Print1()    #相当于Print1(A) a.Print2()    #相当于Print2(a), 请注意@classmethod A.Print3()  
As you can see, Python is passing the "." When a member function is called, the "." The preceding thing is called as the first parameter of a function. And Pyhon doesn't care what we put the name of the first parameter of the member function of the class, we can use any name, we can see the definition of Print3.

The Self,cls,staticmethod,classmethod of Python learning

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.