Python:AttributeError:无法设置属性

时间:2019-10-02 00:58:20

标签: python-3.x

每次我尝试通过Controller类创建Hall类的对象时,都会收到属性错误。我知道我可以在类外部创建对象,但是在类内部却不起作用。我想念什么吗?预先感谢!

    #entity
    class Hall:

        def __init__(self, name, location):
            self.name = name
            self.location = location

        @property
        def name(self):
            return self.name

        @property
        def location(self):
            return self.location

    #boundary
    class UI:

        def uInput():
            value1 = input('Enter name: ')
            value2 = input('Enter location: ')

            return value1, value2

    #controller
    class Controller:

        def add_hall_controller():
            value1, value2 = UI.uInput()
            hall = Hall(value1, value2)

Error message

1 个答案:

答案 0 :(得分:0)

运行上面的代码会导致Attribute error,因为您尝试更改的属性实际上是没有设置器的属性。据我所知,名称和位置不是属性。因此,您可以删除@property装饰器。

class Hall:
  def __init__(self, name, location):
    self.name = name
    self.location = location

  def name(self):
    return self.name

  def location(self):
    return self.location

#boundary
class UI:
  def uInput():
    value1 = input('Enter name: ')
    value2 = input('Enter location: ')
    return [value1, value2]

#controller
class Controller:
  def add_hall_controller():
    value1, value2 = UI.uInput()
    hall = Hall(value1, value2)

Controller.add_hall_controller()