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

What is the difference between instance methods, class methods, and static methods in Python

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

In this issue, the editor will bring you about the differences between instance methods, class methods and static methods in Python. The article is rich in content and analyzes and describes for you from a professional point of view. I hope you can get something after reading this article.

Add decorator-> @ classmethod

Static method: need to add decorator-> @ staticmethod

3. Call:

1. The instance method can be called directly through the object

two。 But when calling with a class name, you need to create an object and pass the object in when passing parameters

3. Class methods can be called directly through the class name or through objects.

4. Static methods can be called directly through the class name or through the object

4. Supplement

1. Static methods cannot be inherited.

two。 Class methods cannot access instance variables, only class variables

For novice rookies who want to more easily learn Python development, Python crawler, data analysis, artificial intelligence and other technologies, here to share a set of systematic teaching resources Add my V:itz992 [tutorials / tools / methods / troubleshooting] class Dog (object): food = "gutou" age = "1" def _ _ init__ (self, name): self.NAME = name def eat (self,age): # instance method # print (self.NAME) print (age) print (self.food) @ classmethod def eat1 (cls) Age): # can only be variables in the class # print (cls.NAME) cls.food = "tang" @ staticmethod def print_1 (): print (Dog.food, Dog.age) these are the differences between instance methods, class methods and static methods in Python shared by the editor If you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

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