0
点赞
收藏
分享

微信扫一扫

python __getitem__用法

class A():
def __init__(self):
self.name = {"key": "aaa"}

def __getitem__(self, item):
return self.name.get(item)

obj = A()
print(obj["key"])

实现迭代
class B():
def __init__(self, a_list):
self.a = a_list
self.other = "hello, world"

def __getitem__(self, index):
return self.a[index]

obj = B(["dog","cat","fish"])
for a in obj:
print(a)

举报

相关推荐

Python turtle用法

0 条评论