簡體   English   中英

為什么我不斷收到此屬性錯誤?

[英]Why do I keep getting this Attribute Error?

每次我運行我的代碼時,它都會彈出消息說“‘ICU’ object 沒有屬性‘_name’。你是說:‘name’嗎?” 我不知道如何解決它。 我已經嘗試更改訪問器和修改器的名稱,但仍然無法弄清楚如何解決它。 有什么建議么?

這是我的代碼:

class Patient:
    def __init__(self, name, age):
        self.name = name
        self.age = age
        self.weight = 150

    @property
    def age(self):
        return self._age

    @age.setter
    def age(self, newValue):
        if newValue > 0:
            self._age = newValue
        else:
            self._age = 0

    @property
    def weight(self):
        return self._weight


    @weight.setter
    def weight(self, newValue):
        if newValue >=0 and newValue <= 1400:
            self._weight = newValue


    #IncreaseAge
    def increaseAge(self):
        self.age = self.age + 1

class In(Patient):
    def __init__(self, name, age, stay):
        self.name = name
        self.age = age
        self.stay = stay

    @property  
    def stay(self):
        return self._stay

    @stay.setter
    def stay(self, value):
        self._name = value

    def __str__(self):
        print("IN-" + self._name + self._age + self.weight + self._stay)

class Out(Patient):
    def __init__(self, name, age):
        self.name = name
        self.age = age

    def __str__(self):
        print("OUT-" + self._name + self._age + self._weight)

class ICU(In):
    def __init__(self, name, age):
        self.name = name
        self.age = age
        self.days = 5 

class CheckUp(Out):
    def __init__(self, name, age):
        self.name = name
        self.age = age 

這是實例的 rest:

# Create three patient objects and print them out
p1 = ICU("Ben Dover", 0)
p2 = ICU("Helen Hywater", -15)
p3 = CheckUp("Amanda Lynn", 45)
p4 = ICU("Chester Minit", 12)
p5 = In("Don Keigh", 89, 10)
p6 = Out("Kay Oss ", 45)
print ("\tStatus\tName\t\tAge\tWeight\tStay")
print ("-" * 55)
print ("p1:\t{}".format(p1))
print ("p2:\t{}".format(p2))
print ("p3:\t{}".format(p3))
print ("p4:\t{}".format(p4))
print ("p5:\t{}".format(p5))
print ("p6:\t{}".format(p6))

print ("-" * 55)

# Change their ages and print them out
p1.age = -5
p2.age = 100
for i in range(6):
    p3.increaseAge()
p4.age = 0
p5.increaseAge()
p6.age = 42

print ("p1:\t{}".format(p1))
print ("p2:\t{}".format(p2))
print ("p3:\t{}".format(p3))
print ("p4:\t{}".format(p4))
print ("p5:\t{}".format(p5))
print ("p6:\t{}".format(p6))
print ("-" * 55)

# Change other instance variables and print them out
p1.weight = 2000
p1.stay = 3
p2.name = "Justin Thyme"
p2.weight = 220
p2.stay = 0
p3.weight = -50
p4.weight = 1400
p5.weight = 0
p5.stay = 21
p6.weight = 1401

print ("p1:\t{}".format(p1))
print ("p2:\t{}".format(p2))
print ("p3:\t{}".format(p3))
print ("p4:\t{}".format(p4))
print ("p5:\t{}".format(p5))
print ("p6:\t{}".format(p6))
print ("-" * 55)

這是因為你的變量名不同。 替換您的代碼:

@stay.setter
def stay(self, value):
    self._name = value

到:

@stay.setter
def stay(self, value):
    self.name = value

在 Python 中,構造函數 - 與所有其他方法一樣 - 可以被覆蓋。 也就是說,一旦您在子類中定義了__init__ ,就永遠不會調用基 class 方法。 這就是導致錯誤的原因。 您需要像這樣顯式調用基數 class:

class ICU(In):
    def __init__(self, name, age):
        self.name = name
        self.age = age
        self.days = 5 
        In.__init__(self, name, age, 10) # stay = 10 since it's not an input parameter in the ICU __init__ method.

這需要在每個 base class 中完成。所以你也會在In class 中做類似的事情。

問題在於“格式”正在您的實例上調用“__ str__”,但是當調用“__ str__”時,您的某些實例沒有“_name”或“_stay”或“_weight”的值...查看每個實例的“__init__”方法,並在看到問題后執行“__str__”。 所以要處理這種情況,您有以下簡單的解決方案

 class In(Patient):
    def __init__(self, name, age, stay):
        self.name = name
        self.age = age
        self.stay = stay

    @property  
    def stay(self):
        return self._stay

    @stay.setter
    def stay(self, value):
        self._name = value

    def __str__(self):
        x = (
            getattr(self, '_name', ''),
            getattr(self, '_age', ''),
            self.weight or ''
            getattr(self, '_stay', ''),
        )
        return ("IN-%s %s %s %s")%(*x)


class Out(Patient):
    def __init__(self, name, age):
        self.name = name
        self.age = age

    def __str__(self):
        x = (
            getattr(self, '_name', ''),
            getattr(self, '_age', ''),
            getattr(self, '_stay', ''),
        )
        return "OUT- %s %s %s"%(*x)

但是你的課程設計得不好,請看下面的一些有趣的東西

class Patient:
    def __init__(self, name, age,  weight=150):
        self._name= name
        self._age = age
        self._weight = weight

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

    @name.setter
    def name(self, value):
        assert isinstance(value, str)
        self._name = value

    @property
    def age(self):
        return self._age

    @age.setter
    def age(self, value):
        assert isinstance(value, int)
        self._age = value

    @property
    def weight(self):
        return self._weight

    @weight.setter
    def weight(self, value):
        assert isinstance(value, int)
        self._weight = value

    def __str__(self):
        return f"{self.__class__.__name__.upper()}-{self.name} {self.age} {self.weight}"


class Out(Patient):
    pass

class In(Patient):
    def __init__(self, name, age, stay, weight=150):
        super().__init__(name, age, weight=weight)
        self._stay = stay

    @property
    def stay(self):
        return self._stay

    @stay.setter
    def stay(self, value):
        assert isinstance(value, int)
        self._stay = value

    def __str__(self):
        return f"{super().__str__()} {self.stay}"

class ICU(In):
    def __init__(self, name, age):
        super().__init__(name, age, 5)

class CheckUp(Out):
    def __init__(self, name, age):
        super().__init__(name, age)

另請注意,您的實例未定義“increaseAge”方法

暫無
暫無

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

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