def getitem 功能:
此功能是当使用带参数的函数调用时,会自动将参数对应到相应的参数:
例如:
class Tag:
def __init__(self):
self.change = {'python': 'This is python','php':'This is php'}
def __getitem__(self, item):
print('这个方法被调用')
return self.change[item]
a = Tag()
print(a['python'])
print(a['php'])
结果:
id:{'python': 'This is python', 'php': 'This is php'}
This is python