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 common magic methods of python?

2025-04-13 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces the relevant knowledge of what the common magic methods of python are, the content is detailed and easy to understand, the operation is simple and fast, and it has a certain reference value. I believe you will gain something after reading this article on common magic methods of python. Let's take a look at it.

Classes and objects

Popular understanding: a class is a template, and an object is an object created through a template

Class (Class) consists of three parts:

Name of the class: class name

Properties of a class: a set of data

Methods of the class: methods (behaviors) that allow operations on

2. Magic methods

In python, there are built-in specific methods called "_ _ xxx__" that are called magic methods automatically when certain operations are performed. Here are several common magic methods.

1. Initialize a class, which is used when creating an instance object to assign values to it.

2. Printing strings _ method: when converting an object to a string str (object) test, print the object's information.

3. Create new _ method: create and return an instance object, call it once, and you will get an object.

4. Get the class of a known object (object .class).

5. Destroy delimited _ method: this method is called by the object when the object destroys the object after the program has finished running, to release resources.

Third, understand self

The self and the object point to the same memory address, so you can think of self as a reference to the object.

# create a class class Car (object): # create a method to print id def getself (self) of self: print ('self=%s'% (id (self) bmw = Car () print (id (bmw)) bmw.getself ()' output 140033867265696 140033867265696''

The so-called self can be understood as the object itself. When an object calls its method, the python interpreter passes the object to self as the first parameter, so the developer only needs to pass the following parameters.

# create a class class Car (object): def _ init__ (self,name,colour): self.name = name self.colour = colour # create a method to print id def getself (self) of self: print ('self=%s'% (id (self)) bmw = Car (' BMW', 'black') # when instantiating an object Self does not require developers to pass parameters, and python automatically passes objects to self print (id (bmw)) bmw.getself () 4. Practice fighting

Play two characters against each other

Import randomimport time# definition class class hero (object): # definition attribute def _ _ init__ (self,name,blood,dblood,ablood): self.name=name# name self.blood=blood# blood volume self.dblood=dblood# this is the reduced blood volume self.ablood=ablood # definition method # Mutual stab def tong (self Enemy): enemy.blood-=self.dblood print (% s cut% s% d blood volume'% (self.name,enemy.name,self.dblood)) def addblood (self): self.blood+=self.ablood print (% s took a blood tonic Added% d blood'% (self.name,self.ablood)) def _ str__ (self): return'% s left% s blood'% (self.name, self.blood) xm = hero ('Ximen blowing Snow', 100 or ygc.blood 20), random.randint (10 or ygc.blood 20)) ygc = hero ('Ye Gu Cheng', 100 brigade. Randint (10 charge 20), random.randint (10 or ygc.blood 20)) x = [1 or ygc.blood 2] while xm.blood > = 0 or ygc.blood > = 0:

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