魔术方法
- 非数学运算
- 字符串表示
__repr__:测试语法调用的方法__str__:print调用的方法
- 集合、序列相关
__len____getitem____setitem____delitem____contains__
- 迭代相关
__iter____next__
- 可调用
__call__
- with 上下文管理器
__enter____exit__
- 数值转换
__abs____bool____int____float____hash____index__
- 元类相关
__new____init__
- 属性相关
__getattr__、__setattr____getattribute__、__setattribute____dir__
- 属性描述符
__get__、__set__、__delete__
- 协程
__await__、__aiter_、__anext__、__aenter__、__aexit__
- 字符串表示
- 数学运算
- 一元运算符
__neg__(-)、__pos__(+)、__abs__
- 二元运算符
__lt__()、__le__()、__eq__()、__gt__()、__ge__()
- 算数运算符
__add__(+)、__sub__(-)、__mul__(*)、__truediv__(/)、__floordiv__(//)、__mod__(%)、__divmod__(divmod())、__pow__(**或pow())、__round__(round())
- 反向算数运算符
__radd__、__rsub__、__rmul__、__rtruediv__、__rfloordiv__、__rmod__、__rdivmod__、__rpow__
- 增量赋值算数运算符
__iadd__、__isub__、__imul__、__itruediv__、__ifloordiv__、__imod__、__ipow__
- 位运算符
__invert__(~)、__lshift__(<<)、__rshift__(>>)、__and__(&)、__or__(|)、__xor__(^)
- 反向运算符
__rlshift__、__rrshift__、__rand__、__ror__、__rxor__
- 增量赋值位运算符
__ilshift__、__irshift__、__iand__、__ior__、__ixor__
- 一元运算符
类和对象
1 | class Student: |
-
自省机制
-
python中对象都具有一个特殊的属性:
__dict__只能查询属于自身的属性1
2stu = Student('zoe', 21)
print(stu.__dict__) -
dir函数,可以查询一个对象中所有的属性和方法,包含这个对象的父类1
print(dir(stu))
-
-
contextlib完成上下文管理器1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22import contextlib
def open_file(file_name):
print(f'open: {file_name}')
# 被 contextmanager 装饰的函数必须是一个生成器函数
# enter 的代码要在 yield 之上
# exit 的代码要在 yield 之x
yield {'name': 'zoe', 'age': 21}
print(f'close: {file_name}')
with open_file('zoe.txt') as f_open:
print("Run...")
"""output
open: zoe.txt
Run...
close: zoe.txt
"""