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 are the methods that define classes in python

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly explains "what are the methods of defining classes in python". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn "what are the methods of defining classes in python?"

There are three definition class methods in python:

Common method

Class method (@ classmethod)

Class method (@ classmethod)

1. Common methods

There are two ways to create a common method (class A () & class B ()).

Class A (): def _ init__ (self, name, age): self.name = name self.age = age def get_name (self): print ('my name is', self.name) def get_age (self): print (Flemi am {self.age} years old') class B (): def get_name (self, name): print ('my name is', name) def get_age (self Age): print (Flemi am {age} years old') if _ _ name__ ='_ _ main__': a = A ('tom',19) a.get_name () # my name is tom a.get_age () # I am 19 years old b = B () b.get_name (' tom') # my name is tom b.get_age (19) # I am 19 years old

In class A (), _ _ init__ () is a special method, which is equivalent to initializing A. the self in _ _ init__ is the object An itself, and name and age are their formal parameters.

The class needs to be instantiated before each method call

2. Class method

@ classmethod no longer needs self to represent itself, but uses cls instead.

There is no need to instantiate at this point. Direct class name. The function name is called.

# Python Learning Exchange Group: 725638078class C (): @ classmethod def get_name (cls, name): print (cls) # print ('my name is% s'% name) @ classmethod def get_age (cls) Age): print (Flemi am% s years old'% age) if _ _ name__ = ='_ main__': C.get_name ('tom') # my name is tom C.get_age (19) # I am 19 years old3, static method

@ staticmethod is also through the class name. Called by the function name

Static methods do not need self or cls that represent their own objects as arguments, just as they do with functions directly.

Class D (): @ staticmethod def get_name (name): print ('my name is% s'% name) @ staticmethod def get_age (age): print (Flemi am% s years old'% age) if _ _ name__ = ='_ main__': D.get_name ('tom') # my name is tom D.get_age (19) # I am 19 years old I believe that you have a deeper understanding of "what are the methods of defining classes in python?" 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

Development

Wechat

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

12
Report