一句话理解python类中__getitem__方法
python类中的__getitem__方法,可以将实例化的类,实现字典dict的键值形式。
class A(object):
def __init__(self):
super(A, self).__init__()
def __getitem__(self, item):
if item == 'xg':
return '西瓜'
elif item == 'rsg':
return '人参果'
a= A()
print(a['xg'])
print(a['rsg'])
西瓜
人参果