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

How to use the class method classmethod of python

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article mainly introduces "how to use the class method classmethod of python". In the daily operation, I believe that many people have doubts about how to use the class method classmethod of python. The editor has consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubt of "how to use the class method classmethod of python". Next, please follow the editor to study!

Definition of @ classmethod

Classmethod is a decorator that specifies that a method in a class is a class binding method.

@ classmethod the difference between decorating or not

The binding method of the instance

The method defined in the class defaults to the instance binding method, and the first parameter of the instance binding method is self,self, which refers to the namespace memory address of the instance. You must have an instance to call.

There are two ways to call: one is "instance. Method name ()", and the other is "class name. method name (instance name)", the premise of the call is that the instance has been created, and no instance call will report an error!

Binding method of the class

Prefix the method defined in the class with @ classmethod to indicate that it is the class's binding method, and the first parameter of the binding method in the class is the cls,cls refers to the namespace memory address of the class. It can be called with or without an instance.

There are two ways to call: one is "class name. method name ()", and the other is "instance. Method name ()". You don't need to create an instance before calling, and you can call it without an instance.

@ classmethod application scenario

There are two common application scenarios, one is to call the binding method of the class to create an instance, and the other is to modify the private properties of the class.

In case 1, call the binding method of the class to create an instance:

Import timeclass Date:def _ init__ (self, year, month, day): self.year = year self.month = month self.day = day @ classmethoddef today (cls): now = time.localtime () return Date (now.tm_year, now.tm_mon, now.tm_mday) today = Date.today () print (f' today is "{today.year} {today.month} month {today.day} day") out: today is "November 9th, 2020"

Explanation:

There are two methods in the above Date class, one is _ _ init__, which is the initialization method of the instance, and the other is today, which is the binding method of the class. You can create the instance without parameters by calling Date.today (). This is often used in actual production. Use the binding method of the heart class to create the instance.

In case 2, the binding method of the class is called to modify the private properties of the class:

Class Goods:__discount = 1 # discount ratio def _ init__ (self, name, price): self.name = name # Commodity name self.price = price # Commodity Price @ propertydef total (self): # Total Commodity Price return self.price*self.__discount @ classmethoddef change_discount (cls, n): Goods.__discount = napple = Goods ('Apple') 5) print (apple.total) Goods.change_discount (0.8) print (apple.total) pear = Goods ('pear', 4) print (pear.total) out:54.03.2

The private attribute discount of the class in the above code controls the discount ratio of goods in the market, and cannot be modified outside the class. You can only modify the private properties of a class by defining its binding methods inside the class.

Note the difference between the properties of the instance and the properties of the class:

The properties of instances are stored in their respective namespaces, so there are as many instances as there are corresponding instance properties.

The properties of the class are stored in the namespace of the class, so there is only one property of the class (no matter how many instances there are).

At this point, the study on "how to use the class method classmethod of python" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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