码迷,mamicode.com
首页 > 编程语言 > 详细

python的__getitem__

时间:2019-02-11 00:57:54      阅读:189      评论:0      收藏:0      [点我收藏+]

标签:方法   obj   整数   int   div   init   类型   相关   print   

#这个方法返回与指定键相关的值。对序列来说,键应该是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__

标签:方法   obj   整数   int   div   init   类型   相关   print   

原文地址:https://www.cnblogs.com/Stephen-Qin/p/10360582.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!