【问题标题】:Class name is not defined类名未定义
【发布时间】:2016-06-18 05:08:55
【问题描述】:
class MyClass:
    sample=0
    w=0
    b=0
    g=0
    o=0
    color_list=['black','white','gray']

    def __init__(self):
        print("Enter Your Name")
        MyClass.name=input()
        print("What's the color of ur car?")
        MyClass.color=input()
        MyClass.sample=MyClass.sample+1

    def check_color(self):
        if MyClass.color in MyClass.color_list:

            if MyClass.color==MyClass.color_list[0]:
                MyClass.b=MyClass.b+1
            elif MyClass.color==MyClass.color_list[1]:
                MyClass.w=MyClass.w+1
            else:
                MyClass.g=MyClass.g+1
        else:
            MyClass.o=MyClass.o+1

    def display_result(self):
        print("Hello :",MyClass.name)
        print("Total number of black cars",MyClass.b)
        print("Total number of white cars",MyClass.w)
        print("Total number of gray cars",MyClass.g)
        print("Other cars",MyClass.o)
        print("Sample Size",MyClass.sample)

    var=0
    mylist=[]
    while var<4:
        mylist.append(MyClass())
        mylist[var].check_color()
        mylist[var].display_result()
        var=var+1

Python IDLE 出错:

==== RESTART: C:/Python34/Practice Programs/TBT Tutorials/Day 4/class.py ====
Traceback (most recent call last):
  File "C:/Python34/Practice Programs/TBT Tutorials/Day 4/class.py", line 1, in <module>
    class MyClass:
  File "C:/Python34/Practice Programs/TBT Tutorials/Day 4/class.py", line 41, in MyClass
    mylist.append(MyClass())
NameError: name 'MyClass' is not defined

【问题讨论】:

  • 我不确定我是否修复了您的缩进,因为它看起来代码的主体在类定义中,如果我没有做对,请edit您的问题。
  • 使用self 引用实例,实例可以有多个。 MyClass 指的是类本身,在定义类之前不可用。你还有很多类范围的代码,这可能不是你想要的。

标签: python class


【解决方案1】:

在你的类中使用self 而不是 MyClass

【讨论】:

    【解决方案2】:

    你在你的classbody中引用你的类:

    class Myclass:
    
        ...
        mylist = []
        mylist.append(Myclass)
    

    但是在执行你的类主体时没有定义术语Myclass

    好像你把缩进弄混了,应该是

    class Myclass:
        ...
    mylist = []
    mylist.append(Myclass)
    

    这会起作用。

    此外,您应该了解类和类的对象之间的区别,因为代码可能不是您想要的。

    【讨论】:

      【解决方案3】:

      问题在于最后一段代码的缩进 - 就目前而言,它已成为 Myclass 的一部分,它正在定义中,还不存在!

      要解决这个问题,只需将其取消一级:

      class MyClass:
          sample=0
          w=0
          b=0
      
          ...
      
              else:
                  MyClass.o=MyClass.o+1
      
          def display_result(self):
              print("Hello :",MyClass.name)
              print("Total number of black cars",MyClass.b)
              print("Total number of white cars",MyClass.w)
              print("Total number of gray cars",MyClass.g)
              print("Other cars",MyClass.o)
              print("Sample Size",MyClass.sample)
      
      # unindent these lines
      var=0
      mylist=[]
      while var<4:
          mylist.append(MyClass())
          mylist[var].check_color()
          mylist[var].display_result()
          var=var+1
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-01-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-02-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多