__repr__(self)
__str__(self)
__bytes__(self)
__hash__(self)
__bool__(self)
__format__(self, format_spec)
__getattr__(self, name)
、__getattribute__(self, name)
、__setattr__(self, name, value)
、__delattr__(self, name)
__dir__(self)
__get__(self, instance, owner)
__set__(self, instance, value)
__delete__(self, instance)
__enter__(self)
__exit__(self, exc_type, exc_value, traceback)
__getitem__(self, key)
__setitem__(self, key, value)
__delitem__(self, key)
__iter__(self)
__reversed__(self)
__eq__(self, other)
__ne__(self, other)
__divmod__(self, other)
__pow__(self, other[, modulo])
__lshift__(self, other)
__rshift__(self, other)
__and__(self, other)
__xor__(self, other)
__or__(self, other)
__radd__(self, other)
__rsub__(self, other)
__rtruediv__(self, other)
__rfloordiv__(self, other)
__rmod__(self, other)
__rdivmod__(self, other)
__rpow__(self, other)
__rlshift__(self, other)
__rrshift__(self, other)
__rand__(self, other)
__rxor__(self, other)
__ror__(self, other)
__iadd__(self, other)
__isub__(self, other)
__imul__()
、__mul__()
、__rmul__()
__itruediv__(self, other)
__ifloordiv__(self, other)
__imod__(self, other)
__ipow__(self, other[, modulo])
__ilshift__(self, other)
__irshift__(self, other)
__iand__(self, other)
__ixor__(self, other)
__ior__(self, other)
__pos__(self)
__neg__(self)
__abs__(self)
__invert__(self)
__complex__(self)
__int__(self)
__float__(self)
__round__(self)
__index__(self)
__next__()
__delslice__()
、__getslice__()
、__setslice__()
增加切片功能__reduce__()
__reduce_ex__()
__subclasshook__()
分类 | 方法 | 说明 | 作用 |
---|---|---|---|
基本 | __repr__(self) |
实例描述,定义当被 repr() 调用时的行为 | |
基本 | __str__(self) |
被print默认调用,定义当被 str() 调用时的行为 | |
基本 | __bytes__(self) |
定义当被 bytes() 调用时的行为 | |
基本 | __hash__(self) |
定义当被 hash() 调用时的行为 | |
基本 | __bool__(self) |
定义当被 bool() 调用时的行为,应该返回 True 或 False | |
基本 | __format__(self, format_spec) |
格式化输出,定义当被 format() 调用时的行为 | |
属性相关 | __getattr__(self, name) |
定义当用户试图获取一个不存在的属性时的行为 | |
属性相关 | __getattribute__(self, name) |
属性访问截断器,定义当该类的属性被访问时的行为 | |
属性相关 | __setattr__(self, name, value) |
定义当一个属性被设置时的行为 | |
属性相关 | __delattr__(self, name) |
定义当一个属性被删除时的行为 | |
属性相关 | __dir__(self) |
定义当 dir() 被调用时的行为 | |
属性相关 | __get__(self, instance, owner) |
定义当描述符的值被取得时的行为 | |
属性相关 | __set__(self, instance, value) |
定义当描述符的值被改变时的行为 | |
属性相关 | __delete__(self, instance) |
定义当描述符的值被删除时的行为 | |
上下文管理器 | __enter__(self) |
对一个对象使用with方法来处理工作前的准备,定义当使用 with 语句进入上下文管理器执行的方法 ,enter 的返回值被 with 语句的目标或者 as 后的名字绑定 | |
上下文管理器 | __exit__(self, exc_type, exc_value, traceback) |
对一个对象使用with方法来处理工作后的清扫行为,当with中的代码块执行完毕后调用的方法。一般被用来处理异常,清除工作或者做一些代码块执行完毕之后的日常工作 | |
容器类型数据 | __getitem__(self, key) |
定义获取容器中指定元素的行为,相当于 self[key] | |
容器类型数据 | __setitem__(self, key, value) |
定义设置容器中指定元素的行为,相当于 self[key] = value | |
容器类型数据 | __delitem__(self, key) |
定义删除容器中指定元素的行为,相当于 del self[key] | |
容器类型数据 | __iter__(self) |
将一个对象模拟成序列,定义当迭代容器中的元素的行为 | |
容器类型数据 | __reversed__(self) |
定义当被 reversed() 调用时的行为 | |
算术运算符 | __divmod__(self, other) |
定义当被 divmod() 调用时的行为 | |
算术运算符 | __pow__(self, other[, modulo]) |
定义当被 power() 调用或 ` 运算时的行为 | |
算术运算符 | __lshift__(self, other) |
定义按位左移位的行为:<< | |
算术运算符 | __rshift__(self, other) |
定义按位右移位的行为:>> | |
算术运算符 | __and__(self, other) |
定义按位与操作的行为:& | |
算术运算符 | __xor__(self, other) |
定义按位异或操作的行为:^ | |
算术运算符 | __or__(self, other) |
定义按位或操作的行为: | |
反运算 | __radd__(self, other) |
(与上方相同,当左操作数不支持相应的操作时被调用) | |
反运算 | __rsub__(self, other) |
(与上方相同,当左操作数不支持相应的操作时被调用) | |
反运算 | __rmul__(self, other) |
(与上方相同,当左操作数不支持相应的操作时被调用) | |
反运算 | __rtruediv__(self, other) |
(与上方相同,当左操作数不支持相应的操作时被调用) | |
反运算 | __rfloordiv__(self, other) |
(与上方相同,当左操作数不支持相应的操作时被调用) | |
反运算 | __rmod__(self, other) |
(与上方相同,当左操作数不支持相应的操作时被调用) | |
反运算 | __rdivmod__(self, other) |
(与上方相同,当左操作数不支持相应的操作时被调用) | |
反运算 | __rpow__(self, other) |
(与上方相同,当左操作数不支持相应的操作时被调用) | |
反运算 | __rlshift__(self, other) |
(与上方相同,当左操作数不支持相应的操作时被调用) | |
反运算 | __rrshift__(self, other) |
(与上方相同,当左操作数不支持相应的操作时被调用) | |
反运算 | __rand__(self, other) |
(与上方相同,当左操作数不支持相应的操作时被调用) | |
反运算 | __rxor__(self, other) |
(与上方相同,当左操作数不支持相应的操作时被调用) | |
反运算 | __ror__(self, other) |
(与上方相同,当左操作数不支持相应的操作时被调用) | |
增量赋值运算 | __iadd__(self, other) |
定义赋值加法的行为:+= | |
增量赋值运算 | __isub__(self, other) |
定义赋值减法的行为:-= | |
增量赋值运算 | __imul__(self, other) |
定义赋值乘法的行为:*= | |
增量赋值运算 | __itruediv__(self, other) |
定义赋值真除法的行为:/= | |
增量赋值运算 | __ifloordiv__(self, other) |
定义赋值整数除法的行为://= | |
增量赋值运算 | __imod__(self, other) |
定义赋值取模算法的行为:百分号= | |
增量赋值运算 | __ipow__(self, other[, modulo]) |
定义赋值幂运算的行为:`= | |
增量赋值运算 | __ilshift__(self, other) |
定义赋值按位左移位的行为:<<= | |
增量赋值运算 | __irshift__(self, other) |
定义赋值按位右移位的行为:>>= | |
增量赋值运算 | __iand__(self, other) |
定义赋值按位与操作的行为:&= | |
增量赋值运算 | __ixor__(self, other) |
定义赋值按位异或操作的行为:^= | |
增量赋值运算 | __ior__(self, other) |
定义赋值按位或操作的行为: | = |
一元操作符 | __pos__(self) |
定义正号的行为:+x | |
一元操作符 | __neg__(self) |
定义负号的行为:-x | |
一元操作符 | __abs__(self) |
定义当被 abs() 调用时的行为 | |
一元操作符 | __invert__(self) |
定义按位求反的行为:~x | |
类型转换 | __complex__(self) |
定义当被 complex() 调用时的行为(需要返回恰当的值) | |
类型转换 | __int__(self) |
定义当被 int() 调用时的行为(需要返回恰当的值) | |
类型转换 | __float__(self) |
定义当被 float() 调用时的行为(需要返回恰当的值) | |
类型转换 | __round__(self) |
定义当被 round() 调用时的行为(需要返回恰当的值) | |
类型转换 | __index__(self) |
1. 当对象是被应用在切片表达式中时,实现整形强制转换 | |
类型转换 | __index__(self) |
2. 如果你定义了一个可能在切片时用到的定制的数值型,你应该定义 index | |
类型转换 | __index__(self) |
3. 如果 index 被定义,则 int 也需要被定义,且返回相同的值 | |
__next__() |
将一个对象模拟成序列 | ||
__delslice__() |
增加切片功能 | ||
__getslice__() |
增加切片功能 | ||
__setslice__() |
增加切片功能 | ||
__reduce__() |
|||
__reduce_ex__() |
|||
__subclasshook__() |
__repr__(self)
class Dog():
def __init__(self, name):
self.name = name
# 实例描述(调试和开发用)
# 如果不重写__repr__()
# print(my_dog)
# 返回<类名+object at+内存地址>
# 返回<__main__.Dog object at 0x000001C4AFE1A164>
def __repr__(self):
return f'<Dog类{self.name}>__repr__......'
if __name__ == "__main__":
my_dog = Dog('A')
print(my_dog)
<Dog类A>__repr__......
__str__(self)
class Dog():
def __init__(self, name):
self.name = name
# 实例描述(为最终用户创建输出)
# 如果不重写__str__()
# print(my_dog)
# 返回<类名+object at+内存地址>
# 返回<__main__.Dog object at 0x000001C4AFE1A164>
def __str__(self):
return f'<Dog类{self.name}>__str__'
if __name__ == "__main__":
my_dog = Dog('A')
print(my_dog)
<Dog类A>__str__
__bytes__(self)
__hash__(self)
__bool__(self)
__format__(self, format_spec)
In [6]: _formats = {'ymd' : '{d.year}-{d.month}-{d.day}',
...: 'mdy' : '{d.month}/{d.day}/{d.year}'}
In [12]: class Date:
...: def __init__(self, year, month, day):
...: self.year = year
...: self.month = month
...: self.day = day
...: def __format__(self, code):
...: if code == '':
...: code ='ymd'
...: fmt = _formats[code]
...: return fmt.format(d=self)
In [14]: d = Date(2018, 8, 8)
In [15]: format(d)
Out[15]: '2018-8-8'
__getattr__(self, name)
、__getattribute__(self, name)
、__setattr__(self, name, value)
、__delattr__(self, name)
class Dog():
def __init__(self, name):
self.name = name
self.age = 0
# 定义了可以控制属性
# print(my_dog.name)
def __getattr__(self, attr):
if attr in ('name', 'age', 'score'):
super().__getattr__(attr)
return attr
else:
return None
# 定义了可以控制属性
# print(my_dog.name = "new_name")
# print(my_dog.name)
def __setattr__(self, name, value):
print("__setattr__:"+str(name)+":"+str(value))
super().__setattr__(name, value)
# 定义了可以控制属性
def __delattr__(self, name):
print("__delattr__"+":"+str(name))
super().__delattr__(name)
if __name__ == "__main__":
my_dog = Dog('A') # __setattr__:name:A
# __setattr__:age:0
my_dog.age = 3 # __setattr__:age:3
print(my_dog.name) # A
print(my_dog.names) # None
print(my_dog.age) # age
del my_dog.age # __delattr__:age
print(my_dog.age) # 报错
__dir__(self)
__get__(self, instance, owner)
__set__(self, instance, value)
__delete__(self, instance)
__enter__(self)
__exit__(self, exc_type, exc_value, traceback)
__getitem__(self, key)
__setitem__(self, key, value)
__delitem__(self, key)
__iter__(self)
__reversed__(self)
__eq__(self, other)
__ne__(self, other)
__divmod__(self, other)
__pow__(self, other[, modulo])
__lshift__(self, other)
__rshift__(self, other)
__and__(self, other)
__xor__(self, other)
__or__(self, other)
__radd__(self, other)
__rsub__(self, other)
__rtruediv__(self, other)
__rfloordiv__(self, other)
__rmod__(self, other)
__rdivmod__(self, other)
__rpow__(self, other)
__rlshift__(self, other)
__rrshift__(self, other)
__rand__(self, other)
__rxor__(self, other)
__ror__(self, other)
__iadd__(self, other)
__isub__(self, other)
__imul__()
、__mul__()
、__rmul__()
import numpy as np
a = np.reshape(np.arange(1, 5), (2, 2))
b = np.reshape(np.arange(4, 0, -1), (2, 2))
"""
a:
[[1 2]
[3 4]]
b:
[[4 3]
[2 1]]
"""
print(a.__matmul__(b))
print(a.__rmatmul__(b))
输出结果为:
[[ 8 5]
[20 13]]
[[13 20]
[ 5 8]]
可以看到,a.matmul(b)的作用就是返回a、b矩阵乘积的结果,而a.rmatmul(b)返回的是b、a矩阵乘积的结果。
a.matmul(b)的作用跟np.matmul(a, b)相同,而a.rmatmul(b)的作用与np.matmul(b, a)相同。
然后,imatmul()函数在我使用的python3.8版本中已经取消。
取而代之的是imul()函数,同时,还有mul()、rmul(),这三个函数的作用相同,都是计算a与b矩阵的内积,这也是字母i的含义,i表示in-place
测试代码如下:
print(a.__rmul__(b))
print(a.__mul__(b))
print(a.__imul__(b))
输出结果为:
[[4 6]
[6 4]]
[[4 6]
[6 4]]
[[4 6]
[6 4]]
__itruediv__(self, other)
__ifloordiv__(self, other)
__imod__(self, other)
__ipow__(self, other[, modulo])
__ilshift__(self, other)
__irshift__(self, other)
__iand__(self, other)
__ixor__(self, other)
__ior__(self, other)
__pos__(self)
__neg__(self)
__abs__(self)
__invert__(self)
__complex__(self)
__int__(self)
__float__(self)
__round__(self)
__index__(self)
__next__()
__delslice__()
、__getslice__()
、__setslice__()
增加切片功能只限python2.x版本,python3.x无此功能
## coding=utf-8
class Foo(object):
save_list = [1, 2, 3, 4, 5, 6, 34, 23, 5]
def __getslice__(self, i, j):
print "__getslice__:"
return self.save_list[i: j]
def __setslice__(self, i, j, sequence):
print "__setslice__:"
self.save_list[i:j] = sequence
print self.save_list
def __delslice__(self, i, j):
print "__delslice__:"
del self.save_list[i: j]
print self.save_list
obj = Foo()
result = obj[1: 2]
print(result)
obj[0: 1] = [23, 22, 15, 4]
del obj[4:]
"""
结果:
__getslice__:
[2]
__setslice__:
[23, 22, 15, 4, 2, 3, 4, 5, 6, 34, 23, 5]
__delslice__:
[23, 22, 15, 4]
"""
__reduce__()
__reduce_ex__()
__subclasshook__()