Article Directory
Operation-related magic methods
think:
class Person:
def __init__(self,name,age):
self.name = name
self.age = age
p1 = Person('zhangsan',18)
p2 = Person('zhangsan',18)
print(p1 == p2)
In the above code, using the == operator to compare two objects, is the result True or False? What exactly is the comparison?
Comparison operator-related magic methods
class Student:
def __init__(self, name, age):
self.name = name
self.age = age
def __eq__(self, other):
return self.name == other.name and self.age == other.age
# def __ne__(self, other):
def __lt__(self, other):
return self.age < other.age
# def __gt__(self, other):
def __le__(self, other):
return self.age <= other.age
# def __ge__(self, other):
s1 = Student('zhangsan', 18)
s2 = Student('zhangsan', 18)
s3 = Student('lisi', 20)
print(s1 == s2)
print(s1 != s2)
print(s1 > s2)
print(s1 >= s2)
print(s1 <= s2)
print(s1 <= s2)
Magic methods related to arithmetic operators
class Student:
def __init__(self, name, age):
self.name = name
self.age = age
def __add__(self, other):
return self.age + other
def __sub__(self, other):
return self.age - other
def __mul__(self, other):
return self.age * other
def __truediv__(self, other):
return self.age / other
def __mod__(self, other):
return self.age % other
def __pow__(self, power, modulo=None):
return self.age ** power
s = Student('zhangsan', 18)
print(s + 1) # 19
print(s - 2) # 16
print(s * 2) # 36
print(s / 5) # 3.6
print(s % 5) # 3
print(s ** 2) # 324
Type conversion related magic methods
class Student:
def __init__(self, name, age):
self.name = name
self.age = age
def __int__(self):
return self.age
def __float__(self):
return self.age * 1.0
def __str__(self):
return self.name
def __bool__(self):
return self.age > 18
s = Student('zhangsan', 18)
print(int(s))
print(float(s))
print(str(s))
print(bool(s))
if s:
print('hello')
Advanced case
[Python] Python realizes the word guessing game-challenge your intelligence and luck!
[python] Python tkinter library implements GUI program for weight unit converter
[python] Use Selenium to get (2023 Blog Star) entries
Use Tencent Cloud Cloud studio to realize scheduling Baidu AI to realize text recognition
[Xiaobai must see] Python word cloud generator detailed analysis and code implementation
[Xiaobai must see] Python crawls an example of NBA player data
[Must-see for Xiaobai] Sample code for crawling and saving Himalayan audio using Python
[Xiaobai must see] Python crawler data processing and visualization
[Must-see for Xiaobai] Python crawler program to easily obtain hero skin pictures of King of Glory
[Must-see for Xiaobai] Use Python to generate a personalized list Word document
Geospatial data processing and visualization using geopandas and shapely (.shp)
Use selenium to crawl Maoyan movie list data
Detailed explanation of the principle and implementation of image enhancement algorithm Retinex
Getting Started Guide to Crawlers (8): Write weather data crawler programs for visual analysis
Getting started with crawlers (4): The best way to crawl dynamic web pages using Selenium and API
Getting Started Guide to Crawlers (3): Python network requests and common anti-crawler strategies
Getting started with crawlers (2): How to use regular expressions for data extraction and processing
Getting started with reptiles (1): Learn the basics and skills of reptiles
Python object-oriented programming basics and sample code
MySQL database operation guide: learn how to use Python to add, delete, modify and query operations
Python file operation guide: encoding, reading, writing and exception handling
Selenium Automation Toolset - Complete Guide and Tutorials
Python web crawler basics advanced to actual combat tutorial
Pandas data processing and analysis tutorial: from basics to actual combat
Detailed explanation of commonly used data types and related operations in Python
[Latest in 2023] Detailed Explanation of Six Major Schemes to Improve Index of Classification Model