Python原生高阶用法

new

静态方法,传入cls类,返回self实例。在__init__之前调用。

init

接收__new__返回的实例,对实例进行初始化。

call

使实例化后的对象变成了callable可以调用的对象()。同时

1
2
3
4
5
6
7
8
9
10
11
class Hello:
def __call__(self, arg):
print('hello' + arg)

def hello():
print('hello word')
h = Hello()
h('world')
# hello world
h.hello.__call__()
# hello world

getitem

getitem可以直接对对象进行取值。

1
2
def __getitem__(self, idx):
return self.data[idx]

反射

Python也有反射,即可以通过字符串的形式操作实例的属性。

__hasattr__, __getattr__, __setattr__, __delattr__

类变量

1
2
3
4
class C:
var_class = 0
def __init__(self):
self.var_instance = 1

类似静态成员。


Reference

https://www.jianshu.com/p/3aca78a84def