Python Classmethod modifier

Source: Internet
Author: User
Tags modifier python classmethod

Describe

The function that corresponds to the Classmethod modifier does not need to be instantiated, does not require the self parameter, but the first parameter needs to be a CLS parameter that represents its own class, can invoke the class's properties, the class's methods, instantiate the object, and so on.

Grammar

Classmethod Syntax:

Classmethod

Parameters

No

return value

Returns the class method of the function.

Instance

The following examples show how Classmethod is used:

#!/usr/bin/python

#-*-Coding:utf-8-*-

Class A(object):

bar = 1 def

< Span class= "Hl-identifier" >func1 (self : print ( "foo ' )

< Span class= "Hl-identifier" >< Span class= "Hl-code" > @ classmethod

< Span class= "hl-brackets" > def func2 (cls) :

print (" func2 /span>

print (cls. Bar

CLS(). Func1() # call Foo method A. Func2() # No instances required

The output is:

Func21foo 

Python Classmethod modifier

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.