繁体   English   中英

为什么 function 返回无?

[英]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 ”的子类,其大小设置为“小”。

这里有两个问题:

  1. 你没有从set_size返回任何东西

  2. 您正在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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM