类属性已被声明,为什么会发生错误?

时间:2019-11-22 18:18:35

标签: python python-3.x

我遇到了无法解释的错误。有人可以教我吗?

class Base(object):
    def __init__(self):
        print("Base")

class MyTest(Base):
    base = "Test"
    data = base+"111"                                           #OK
    string_list = [base+str(Value) for Value in range(10)]      #Error! Why?

    def __init__(self):
        super(MyTest, self).__init__()
        print(self.string_list)
        print(self.data)


MyTest()

我收到什么错误消息

Traceback (most recent call last):
  File "Test.py", line 8, in <module>
    class MyTest(Base):
  File "Test.py", line 11, in MyTest
    string_list = [base+str(Value) for Value in range(10)]
  File "Test.py", line 11, in <listcomp>
    string_list = [base+str(Value) for Value in range(10)]
NameError: name 'base' is not defined

0 个答案:

没有答案