ToB企服应用市场:ToB评测及商务社交产业平台

标题: Python中,类的特殊方法与内置函数的关联 [打印本页]

作者: 涛声依旧在    时间: 2023-4-6 19:51
标题: Python中,类的特殊方法与内置函数的关联
目录

Python类

Python类的设计原则

特殊方法【Special methods】

Python中的特殊方法是一系列预定义的方法,允许你为对象上的内置操作定义自定义行为,使用双下划线前缀和后缀来识别,也成为"dunder"方法。这里讲述一些Python类中常用的特殊方法:
<ol>__init__(self, ...): 这是构造方法,对象创建时调用,用于初始化对象的属性并设置其初始状态。
__str__(self): 返回对象的字符串表示,用于创建一个人类可读的字符串表示。
__repr__(self): 返回对象的字符串表示,用于创建可用于创建对象的字符串表示。
__len__(self): 返回对象的长度,供len()函数使用。
__getitem__(self, key): 这个方法允许你以括号的形式访问对象的元素,可以使用[]来访问元素。
__setitem__(self, key, value): 这个方法允许你以括号的形式设置对象元素的值,可以使用[]来修改元素的值。
__delitem__(self, key): 这个方法允许你以括号的形式删除对象的元素。
__add__(self, other): 自定义对象的+操作方式。
__eq__(self, other): 自定义对象的==操作方式。
__lt__(self, other):  自定义对象的 other.agep1 = Person("Alice", 25)p2 = Person("Bob", 30)p3 = Person("Charlie", 20)people = [p1, p2, p3]print(p1)        # Output: Person(name='Alice', age=25)print(min(people).name)  # Output: 'Charlie'print(max(people).name)  # Output: 'Bob'[/code]特殊方法与内置函数的对应:
特殊方法内置函数备注__len__()len()返回对象的长度__getitem__(), __setitem__()[]定义对象元素的访问和修改行为__iter__(), __next__()for等定义对象的迭代行为__contains__()in判断对象是否包含某个特定元素__add__(), __radd__()+定义对象的加法行为__sub__(), __rsub__()-定义对象的减法行为__mul__(), __rmul__()*定义对象的乘法行为__divmod__(), __rdivmod__()divmod()定义对象的余数除法行为__eq__(), __ne__()==, !=定义对象的判等操作__lt__(),  __le__(),
__gt__(), __ge__()=定义对象间的比较操作__hash__()hash()定义对象的哈希值(hash value)English Version

The key design principles for Python classes

Special methods

Special methods in Python are a set of predefined methods that allow you to define custom behavior for built-in operations on your objects. They are identified by their double underscore prefix and suffix, also known as "dunder" methods.
Here are some of the most commonly used special methods in Python:
<ol>__init__(self, ...): This is the constructor method that is called when an object is called. It initializes the object's attributes and sets its initial state.
__str__(self): This methods returns a string representation of the object. It's called by the str() function and by the print() statement.
__repr__(self): This method returns a string representation of the object that can be used to recreate the object. It is called by the repr() function and by the interactive Python shell. The difference between __str__ and __repr__ is that __repr__ is used to create an unambiguous string representation of an object that can be used to recreate the object, while __str__ is used to create a human-readable string representation of an object.
__len__(self): This method returns the length of the object. It is called by the len() function.
__getitem__(self, key): This method allows you to access an item in the object using bracket notation. It is called when you use the bracket operation([]) on the object.
__setitem__(self, key, value): This method allows you to set the value of an item in the object using bracket notation. It is called when you use the bracket operation([]) on the object with an assignment.
__delitem__(self, key): This method allows you to delete an item from the object using bracket notation. It is called when you use the del statement on the object with bracket notation.
__add__(self, other): This method allows you to define how the + operator works with your object. It is called when you use the + operator on the object.
__eq__(self, other): This method allows you to define how the == operator works with your object. It is called when you use the == operator on the object.
__lt__(self, other):  This method allows you to define how the




欢迎光临 ToB企服应用市场:ToB评测及商务社交产业平台 (https://dis.qidao123.com/) Powered by Discuz! X3.4