【问题标题】:How am I able to fix an attribute error when using classes使用类时如何修复属性错误
【发布时间】:2021-12-31 17:56:36
【问题描述】:

编写一个名为“Person”的类,其中包含人名、地址和电话号码的数据属性。

class Person:
    def __init__(self, n, a, p):
        self.name = n
        self.address = a
        self.phone = p
    
    def set_name(self, n):
        self.name = n
    
    def set_address(self, a):
        self.address = a
    
    def set_phone(self, p):
        self.phone = p
    
    def get_name(self):
        return 'Customer Name:'
    
    def get_address(self):
        return 'Customer Address:'
    
    def get_phone(self):
        return 'Customer Phone Number:'
    
def main():
    n = input('Enter Customer Name: ')
    a = input('Enter the Customer Address: ')
    p = input('Enter the Customer Phone Number: ')
    
    print(n.get_person())
    print(a.get_address())
    print(p.get_phone())
main()

如何修复我的属性错误?

【问题讨论】:

    标签: python python-3.x class attributeerror


    【解决方案1】:

    首先,您的错误应该是TypeError,因为您正在从类中调用实例方法:

    >>> class Foo:
    ...     def __init__(self):
    ...         self.bar = 1
    ...     def thing(self):
    ...         return self.bar
    ...
    >>>
    >>>
    >>> Foo.thing()
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: thing() missing 1 required positional argument: 'self'
    

    不过,修复方法是一样的。

    你需要创建一个类的实例来访问它的属性:

    class Foo:
        def __init__(self):
            self.bar = 1
    
    # Foo is a class, but it doesn't have a 
    # bar attribute. An *instance* of the class
    # does
    
    # this raises an attributeError
    Foo.bar
    AttributeError
    
    # this doesn't, because we created an instance
    foo = Foo()
    foo.bar
    1
    

    你需要创建一个person实例,然后你可以得到它的属性:

    class Person:
        def __init__(self, name, address, phone):
            self.name = name
            self.address = address
            self.phone = phone
    
    
    Person.phone 
    # AttributeError
    
    # Create an instance of Person here
    tim = Person(name='Tim', address='1234 Main St', phone='555-5555')
    
    # Then you can get Tim's information
    tim.phone
    '555-5555'
    

    【讨论】:

      【解决方案2】:

      您需要使用 person = Person(n, a, p) 之类的东西创建 Person 类的实例。在 main() 函数的输入语句之后执行此操作。

      其次,打印出属性时,需要引用自己创建的Person类的实例(即person.get_person()、person.get_address()等)。

      最后,在调用 get 函数时,请确保这些函数返回您要查找的值。函数get_address() 应该返回self.address。

      这是我的建议:

      class Person:
          def __init__(self, n, a, p):
              self.name = n
              self.address = a
              self.phone = p
          
          def set_name(self, n):
              self.name = n
          
          def set_address(self, a):
              self.address = a
          
          def set_phone(self, p):
              self.phone = p
          
          def get_name(self):
              return self.name
          
          def get_address(self):
              return self.address
          
          def get_phone(self):
              return self.phone
      
      def main():
          n = input('Enter Customer Name: ')
          a = input('Enter the Customer Address: ')
          p = input('Enter the Customer Phone Number: ')
          
          person = Person(n, a, p) # create the instance
      
          # get attributes of instantiated object
          print(person.get_person())
          print(person.get_address())
          print(person.get_phone())
      
      main()
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-08-14
        • 1970-01-01
        • 1970-01-01
        • 2018-09-27
        • 2021-12-23
        • 2016-02-05
        相关资源
        最近更新 更多