在下面的代码中,类将获得一个列表作为参数。对象有一个“长度”变量(该列表的长度)。
__ len __()方法必须返回“长度”的值
class Gen:
def __init__(self, lst, length ):
self.lst = lst # list
self.length = length
def show(self):
print("List:", self.lst)
def __len__(self):
# will return 'length's value
pass
x = Gen([1, 2, 3, 4])
x.show()https://stackoverflow.com/questions/73353783
复制相似问题