![](/img/trans.png)
[英]Why do I keep getting the following error “NameError” and 'Attribute Error'?
[英]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.