簡體   English   中英

使用類時如何修復屬性錯誤

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

編寫一個名為“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()

如何修復我的屬性錯誤?

首先,您的錯誤確實應該是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'

但是,無論如何修復都是相同的。

您需要創建一個 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

您需要創建一個 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'

您需要使用person = Person(n, a, p)之類的東西創建 Person class 的實例。 main() function 的輸入語句之后執行此操作。

其次,在打印出屬性時,您需要引用您創建的 Person class 的實例(即 person.get_person()、person.get_address() 等)。

最后,在調用 get function 時,請確保這些函數返回您要查找的值。 function 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()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM