python的__getitem__

#这个方法返回与指定键相关的值。对序列来说,键应该是0~n-1的整数,其中n为序列的长度。对映射来说,键可以是任何类型。
class Test(object):
    def __init__(self):
        self.change = {"python": "this is python"}
        self.list = ['hello', '2019']

    def __getitem__(self, item):
        return self.list[item]
        # return self.change[item]

t = Test()
print(t[1])
# print(t['python'])

猜你喜欢

转载自www.cnblogs.com/Stephen-Qin/p/10360582.html
今日推荐