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 Python object-oriented classes and objects

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

Share

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

This article mainly introduces "how to use Python object-oriented classes and objects". In daily operations, I believe many people have doubts about how to use Python object-oriented classes and objects. Xiaobian consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "how to use Python object-oriented classes and objects". Next, please follow the editor to study!

Class definition class

The initials of all class names should be capitalized, and hump nomenclature should be followed for multiple words.

All classes that do not have a parent class defined inherit object

Format: class class name [(parent class)]:

# two ways mean the same class Person: pass class Person (object): pass defines classes and attributes

Name, age, and subject are class attributes

Class Student: # Class attribute name =''age = 0 subject = [] method object method (ordinary method) format: def method name (self [, parm1,parm2...]): pass

Self is the object itself.

Class Student: # Class attribute name = 'student' age = 0 subject = [] # Common method def study (self): print (self.name,' learning...') Xiaoming = Student () xiaoming.name = 'xiaoming'xiaoming.study ()

-

Output:

Xiaoming is learning.

Class Student: # Class attribute name = 'student' age = 0 subject = [] # Common method def study (self, name): print (name,' learning...') S1 = Student () s1.study ('xiaohong')

-

Output:

Xiaohong is learning.

Class method format: @ classmethoddef method name (cls): pass

Class methods use the @ classmethod decorator

Object method passes in self, class method passes in cls

Class methods can be called directly with the class, or with instances (objects) of the class.

Class Dog: dog_type = 'Alaska' def _ init__ (self, nickname): self.name = nickname def dog_run (self): print ('{} running in the park. Looks like a {}. '.format (self.name, self.dog_type) @ classmethod def dog_sit (cls): print (cls.dog_type,' good boy, sit down') dog1 = Dog ('meatballs') dog1.dog_type = 'little Teddy' dog1.dog_run () Dog.dog_sit ()

-

Output:

Meatballs are running in the park. It looks like a little Teddy.

Alaska is good. Sit down.

Static method

Static methods use the decorator @ staticmethod

Static methods have no parameters

Use static methods when methods have nothing to do with class properties and object properties

Support object and class name to call directly

Class Dog: def _ init__ (self, nickname): self.name = nickname @ staticmethod def dog_sit (): print ('good boy, sit down') dog1 = Dog ('meatballs') dog1.dog_sit () Dog.dog_sit ()-

Output:

Very good. Sit down.

Very good. Sit down.

Magic method format: def _ _ method name _ _ (self): pass

_ _ init__ () constructor

If there is no _ _ init__. Directly use the class to apply for the same space as the class from memory and give it to the object.

If there is a _ _ init__. Use the class to request the same memory space as the class, and the memory space (self) is passed to _ _ iniy__, to execute _ _ init__. Finally, memory space is given to the object.

Object creation object

Format: object name = class name ()

Class Student: # Class attribute name =''age = 0 subject = [] xiaoming = Student () object attribute

When an object is created, the object inherits the properties of the class. When you get a property, you also get it from the class.

When the default class attribute is assigned (object name. Property = xxxx), the property becomes an object property.

Class Student: # class attribute name = 'student' age = 0 subject = [] xiaoming = Student () print (' class attribute:', xiaoming.name) # this is the class attribute obtained from the class xiaoming.name = 'xiaoming' # because the assignment becomes the object attribute print (' object attribute:', xiaoming.name)-

Output:

Class attribute: student

Object property: xiaoming

At this point, the study on "how to use Python object-oriented classes and objects" 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: 225

*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