如果类定义了 call 方法,那么它的实例可以作为函数调用。
IndexError 错误类型:
有序表索引超出范围
https://www.cnblogs.com/2bjiujiu/p/9063864.html
raise 关键字:
用于手动抛出异常
http://c.biancheng.net/view/2360.html
LookupError 错误类型:
https://blog.csdn.net/gzlaiyonghao/article/details/6124883
示例:
import random
# # 测试 list.pop
# list1 = ['Google', 'Runoob', 'Taobao']
# list_pop = list1.pop(1) # 默认弹出最末尾(最右)的
# print(list_pop)
class BingoCage:
def __init__(self, items):
self._items = list(items)
random.shuffle(self._items)
def pick(self):
try:
return self._items.pop()
except IndexError:
raise LookupError('pick from empty BingoCage')
def __call__(self):
return self.pick()
bingo = BingoCage(range(3))
print(bingo.pick())
print(bingo())
print(callable(bingo))