TypeError:'MyList'对象不可订阅

时间:2017-07-04 17:28:59

标签: python list class tuples iterable

我正在尝试获取Class对象的长度,MyList的,个人可迭代的,self.it. 2种方法的代码如下所示:

def __init__(self, *args, **kwargs):

    if isinstance(args, (list, tuple)):
        self.it = args 
    else:
        self.it = kwargs

def __len__(self):
    print("len")
    return len(self.it)

当代码命中def__len__(self)时,会产生以下错误 -

TypeError                                 Traceback (most recent call last)
<ipython-input-205-00a18097c81b> in <module>()
      1 # test your implementation of MyList
----> 2 test_list(MyList)

C:\Users\User\Desktop\beaver\Python_5_HW\graders\grader36.pyc in test_list(StdntClass)

C:\Users\User\Desktop\beaver\Python_5_HW\graders\grader36.pyc in test_guantlet(x, y, *args)

TypeError: 'MyList' object is not subscriptable

我在StackOverflow上搜索过这个错误,但我无法弄清楚可能出现的问题。我应该做些什么?

0 个答案:

没有答案