Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Function explanation of cls and self in python

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "the function of cls and self in python". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "the function explanation of cls and self in python".

In general, to use a method of a class, you need to instantiate an object before calling the method.

Using @ staticmethod or @ classmethod, you can directly name the class without instantiation. Called by the method name ().

This helps to organize code, put some functions that should belong to a class into that class, and helps to keep the namespace clean.

Class A (object): a ='a'@ staticmethod def foo1 (name): print 'hello', name def foo2 (self, name): print' hello', name @ classmethod def foo3 (cls, name): print 'hello', name

First define a class A, in which there are three functions, foo1 is a static function, decorated with @ staticmethod decorator, this method has some relationship with the class but does not need to use an instance or class to participate. The following two methods can be output normally, that is, they can be used either as methods of a class or as methods of an instance of a class.

A = A () a.foo1 ('mamq') # output: hello mamqA.foo1 (' mamq') # output: hello mamq

Foo2 is a normal function, a function of an instance of a class, and can only be called through a.

A.foo2 ('mamq') # output: hello mamqA.foo2 (' mamq') # error: unbound method foo2 () must be called with An instance as first argument (got str instance instead)

Foo3 is a class function, and cls is used as the first parameter to represent the class itself. In class methods, class methods are methods that are only related to the class itself and independent of the instance. The following two methods can output normally.

A.foo3 ('mamq') # output: hello mamqA.foo3 (' mamq') # output: hello mamq

However, through the example, it is found that the usage and output of staticmethod and classmethod are the same, and then look at the difference between the two methods.

Since both @ staticmethod and @ classmethod can directly name the class. Method name () to call, so what's the difference between them?

Judging from their use,

@ staticmethod does not need to represent the self of its own object and the cls parameter of its own class, just as it does with a function.

@ classmethod also does not need the self parameter, but the first parameter needs to be the cls parameter that represents its own class.

If you want to call some property methods of this class in @ staticmethod, you can only call the class name directly. The name of an attribute or class. Method name.

Since @ classmethod holds the cls parameter, it can call the properties of the class, the methods of the class, instantiated objects, and so on, to avoid hard coding.

In other words, you can call other methods and properties of the class in classmethod, but staticmethod can only call the properties of the class through A.a, but not by calling A.foo2 () inside the function. Modify the above code to explain:

Class A (object): a ='a'@ staticmethod def foo1 (name): print 'hello', name print A.a # normal print A.foo2 (' mamq') # error: unbound method foo2 () must be called with An instance as first argument (got str instance instead) def foo2 (self, name): print 'hello', name @ classmethod def foo3 (cls, name): print' hello' Name print A.a print cls (). Foo2 (name) here I believe you have a deeper understanding of the "functional explanation of cls and self in python", so you might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report