python的__getitem__

Posted 禾田守望者

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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‘])

 

以上是关于python的__getitem__的主要内容,如果未能解决你的问题,请参考以下文章