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__的主要内容,如果未能解决你的问题,请参考以下文章

Python 类特殊方法__getitem__如何使用?

python学习之__getitem__,__setitem__,__delitem__

如何让 Python 2 的 __getitem__ 在类上工作? [复制]

python - __setitem__/__getitem__/__delitem__类的内置方法

Python for循环与__getitem__的关系记录

python 魔法方法之:__getitem__ __setitem__ __delitem__