[英]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" ?
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)
結論:
black indoor None
<__main__.Tiger object at 0x000002711C6D4DF0> wild big
它從不打印任何內容,因為您從不從該方法返回任何內容。 您需要在方法結束時返回所需的任何內容。
return self.size
在 set_size 方法結束時。
這是一個更好的組織。
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)
但是,這仍然存在問題。 貓的大小不應該由“野生”或“室內”來決定。 Tiger
作為一種特定的Cat
,應始終將其大小設置為“大”。 您可能有另一個名為“ Domestic
”的子類,其大小設置為“小”。
這里有兩個問題:
你沒有從set_size
返回任何東西
您正在Tiger
的 set_size 中重新運行__init__
set_size
這是更正后的代碼:
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)
如果方法中沒有返回,它總是返回 None。
此代碼已稍作修改。
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.