简体   繁体   English

使用类时如何修复属性错误

[英]How am I able to fix an attribute error when using classes

Write a class named 'Person' with data attributes for a person's name, address, and telephone number.编写一个名为“Person”的 class,其中包含人名、地址和电话号码的数据属性。

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()

How am I able to fix my attribute error?如何修复我的属性错误?

First, your errors really should be TypeError , because you are calling an instance method from the class:首先,您的错误确实应该是TypeError ,因为您正在从 class 调用实例方法:

>>> 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'

However, the fix is the same regardless.但是,无论如何修复都是相同的。

You need to create an instance of a class to access its attributes:您需要创建一个 class 的实例来访问其属性:

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

You need to create a person instance, then you can get its attributes:您需要创建一个 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'

You need to create an instance of the Person class using something like person = Person(n, a, p) .您需要使用person = Person(n, a, p)之类的东西创建 Person class 的实例。 Do this after the input statements of your main() function.main() function 的输入语句之后执行此操作。

Secondly, when printing out the attributes, you need to reference the instance of the Person class that you created (ie. person.get_person(), person.get_address(), etc).其次,在打印出属性时,您需要引用您创建的 Person class 的实例(即 person.get_person()、person.get_address() 等)。

Finally, when calling the get function, make sure those functions are returning the value that you're looking for.最后,在调用 get function 时,请确保这些函数返回您要查找的值。 The function get_address() should return self.address. function get_address()应该返回 self.address。

Here is my suggestion:这是我的建议:

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()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我会收到属性错误? 我该如何解决? - Why am I getting an attribute error? How can I fix it? 如何修复错误&#39;AttributeError:模块&#39;board&#39;没有属性&#39;SCK&#39;我正在使用树莓派 - How to fix the error 'AttributeError: module 'board' has no attribute 'SCK'' I am using raspberry pi 我收到类中定义的属性的属性错误。 我怎样才能解决这个问题? - I am getting an attribute error for an attribute defined inside the class. How can I fix this? 当我尝试添加字典键和值时如何修复此代码 - How am I able to fix this code when I try to add a dictionary key and value 为什么我收到错误错误:当我使用 Pandas 时,模块 'string' 没有属性 'lower'? - Why I am receiving the error Error: module ‘string’ has no attribute ‘lower’ when I am using Pandas? 即使使用顺序 model,我也会收到“AttributeError: 'Model' object has no attribute 'predict_classes'” - Even when using Sequential model, I am getting “AttributeError: 'Model' object has no attribute 'predict_classes' ” 在python中使用熊猫时如何解决“属性错误” - How to fix “Attribute error” when using pandas in python 我收到 delete_message 的属性错误,我应该如何解决这个问题? - I am getting an Attribute Error for delete_message, how should I fix this? 当我使用 twilio API 时,如何在 AWS Lambda 函数中使用 python 修复超时错误 - How to fix timed out error using python in AWS Lambda functions when i am using twilio API's 我收到“SystemExit: 2”错误,如何修复 - I am getting 'SystemExit: 2' error , how to fix it
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM