|
方法名 |
重载说明 | 运算符调用方式 |
| __init__ | 构造函数 | 对象创建: X = Class(args) |
| __del__ | 析构函数 | X对象收回 |
| __add__/__sub__ | 加减运算 | X+Y, X+=Y/X-Y, X-=Y |
| __or__ | 运算符| | X|Y, X|=Y |
| _repr__/__str__ | 打印/转换 | print(X)、repr(X)/str(X) |
| __call__ | 函数调用 | X(*args, **kwargs) |
| __getattr__ | 属性引用 | X.undefined |
| __setattr__ | 属性赋值 | X.any=value |
| __delattr__ | 属性删除 | del X.any |
| __getattribute__ | 属性获取 | X.any |
| __getitem__ | 索引运算 | X[key],X[i:j] |
| __setitem__ | 索引赋值 | X[key],X[i:j]=sequence |
| __delitem__ | 索引和分片删除 | del X[key],del X[i:j] |
| __len__ | 长度 | len(X) |
| __bool__ | 布尔测试 | bool(X) |
| __lt__, __gt__, __le__, __ge__, __eq__, __ne__ | 特定的比较 | 依次为X<Y,X>Y,X<=Y,X>=Y, X==Y,X!=Y 注释:(lt: less than, gt: greater than, le: less equal, ge: greater equal, eq: equal, ne: not equal ) |
| __radd__ | 右侧加法 | other+X |
| __iadd__ | 实地(增强的)加法 | X+=Y(or else __add__) |
| __iter__, __next__ | 迭代 | I=iter(X), next() |
| __contains__ | 成员关系测试 | item in X(X为任何可迭代对象) |
| __index__ | 整数值 | hex(X), bin(X), oct(X) |
| __enter__, __exit__ | 环境管理器 | with obj as var: |
| __get__, __set__, __delete__ | 描述符属性 | X.attr, X.attr=value, del X.attr |
| __new__ | 创建 | 在__init__之前创建对象 |
>>> class Human():
... def __init__(self, n):
... self.name = n
... print("__init__ ",self.name)
... def __del__(self):
... print("__del__")
...
>>> h = Human('Tim')
__init__ Tim
>>> h = 'a'
__del__
>>> class Computation(): ... def __init__(self,value): ... self.value = value ... def __add__(self,other): ... return self.value + other ... def __sub__(self,other): ... return self.value - other ... >>> c = Computation(5) >>> c + 5 10 >>> c - 3 2
>>> class Str(object): ... def __str__(self): ... return "__str__ called" ... def __repr__(self): ... return "__repr__ called" ... >>> s = Str() >>> print(s) __str__ called >>> repr(s) '__repr__ called' >>> str(s) '__str__ called'
>>> class Indexer:
data = [1,2,3,4,5,6]
def __getitem__(self,index):
return self.data[index]
def __setitem__(self,k,v):
self.data[k] = v
print(self.data)
>>> i = Indexer()
>>> i[0]
1
>>> i[1:4]
[2, 3, 4]
>>> i[0]=10
[10, 2, 3, 4, 5, 6]
class A():
def __init__(self,ax,bx):
self.a = ax
self.b = bx
def f(self):
print (self.__dict__)
def __getattr__(self,name):
print ("__getattr__")
def __setattr__(self,name,value):
print ("__setattr__")
self.__dict__[name] = value
a = A(1,2)
a.f()
a.x
a.x = 3
a.f()
__setattr__
__setattr__
{'a': 1, 'b': 2}
__getattr__
__setattr__
{'a': 1, 'x': 3, 'b': 2}
>>> class Indexer: ... data = [1,2,3,4,5,6] ... def __getitem__(self,index): ... return self.data[index] ... >>> x = Indexer() >>> for item in x: ... print(item) ... 1 2 3 4 5 6
class Next(object):
def __init__(self, data=1):
self.data = data
def __iter__(self):
return self
def __next__(self):
print("__next__ called")
if self.data > 5:
raise StopIteration
else:
self.data += 1
return self.data
for i in Next(3):
print(i)
print("-----------")
n = Next(3)
i = iter(n)
while True:
try:
print(next(i))
except Exception as e:
break
__next__ called 4 __next__ called 5 __next__ called 6 __next__ called ----------- __next__ called 4 __next__ called 5 __next__ called 6 __next__ called
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有