[英]Why function returns None?
Why does the cat1.set_size() function return None instead of "small" and the cat2.color function returns "<__ main __. Tiger object at 0x000002711C6D4DF0>" instead of "white" ? Why does the cat1.set_size() function return None instead of "small" and the cat2.color function returns "<__ main __. Tiger object at 0x000002711C6D4DF0>" instead of "white" ?
class Cat:
def __init__(self, color, cat_type):
self.size = "undefined"
self.color = color
self.cat_type = cat_type
def set_size(self, cat_type):
if self.cat_type == "indoor":
self.size = "small"
else:
pass
class Tiger(Cat):
def __init__(self, color, cat_type):
super().__init__(color, cat_type)
def set_size(self, cat_type):
super().__init__(self, cat_type)
if self.cat_type == "wild":
self.size = "big"
else:
self.size = "undefined"
cat1 = Cat(color="black", cat_type="indoor")
cat1_size = cat1.set_size("indoor")
print(cat1.color, cat1.cat_type, cat1_size)
cat2 = Tiger(color="white", cat_type="wild")
cat2.set_size("wild")
print(cat2.color, cat2.cat_type, cat2.size)
Conclusion:结论:
black indoor None
<__main__.Tiger object at 0x000002711C6D4DF0> wild big
It never prints something because you never return anything from the method.它从不打印任何内容,因为您从不从该方法返回任何内容。 You need to return whatever you need at the end of the method.您需要在方法结束时返回所需的任何内容。
return self.size
At the end of set_size method.在 set_size 方法结束时。
Here's a better organization.这是一个更好的组织。
class Cat:
def __init__(self, color, cat_type):
self.size = "undefined"
self.color = color
self.cat_type = cat_type
def set_size(self):
if self.cat_type == "indoor":
self.size = "small"
class Tiger(Cat):
def set_size(self):
if self.cat_type == "wild":
self.size = "big"
cat1 = Cat(color="black", cat_type="indoor")
cat1.set_size()
print(cat1.color, cat1.cat_type, cat1.size)
cat2 = Tiger(color="white", cat_type="wild")
cat2.set_size()
print(cat2.color, cat2.cat_type, cat2.size)
This still has problems, though.但是,这仍然存在问题。 The size of the cat should not be determined by "wild" or "indoor".猫的大小不应该由“野生”或“室内”来决定。 A Tiger
, as a specific kind of Cat
, should always have its size set to "big". Tiger
作为一种特定的Cat
,应始终将其大小设置为“大”。 You might have another subclass called Domestic
that has its size set to "small".您可能有另一个名为“ Domestic
”的子类,其大小设置为“小”。
There are two problems here:这里有两个问题:
You are not returning anything from set_size
你没有从set_size
返回任何东西
You are rerunning the __init__
function in Tiger
's set_size
您正在Tiger
的 set_size 中重新运行__init__
set_size
Here is the corrected code:这是更正后的代码:
class Cat:
def __init__(self, color, cat_type):
self.size = "undefined"
self.color = color
self.cat_type = cat_type
def set_size(self):
if self.cat_type == "indoor":
self.size = "small"
return self.size
class Tiger(Cat):
def __init__(self, color, cat_type):
super().__init__(color, cat_type)
def set_size(self):
super().set_size()
if self.cat_type == "wild":
self.size = "big"
return self.size
cat1 = Cat(color="black", cat_type="indoor")
cat1_size = cat1.set_size("indoor")
print(cat1.color, cat1.cat_type, cat1_size)
cat2 = Tiger(color="white", cat_type="wild")
cat2.set_size("wild")
print(cat2.color, cat2.cat_type, cat2.size)
If there is no return in the method it always returns None.如果方法中没有返回,它总是返回 None。
This code has been slightly modified.此代码已稍作修改。
class Cat:
def __init__(self, color, cat_type):
self.size = "undefined"
self.color = color
self.cat_type = cat_type
def set_size(self):
if self.cat_type == 'indoor':
self.size = 'small'
return self.size
class Tiger(Cat):
def __init__(self, color, cat_type):
super().__init__(color, cat_type)
def set_size(self):
super().set_size()
if (self.cat_type == 'wild'):
self.size = 'big'
return self.size
cator = Cat(color='black', cat_type='indoor')
cator_size = cator.set_size()
print("Cat is {}, {}, {}".format(cator.color, cator.cat_type, cator_size))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.