【问题标题】:Why function returns None?为什么函数返回无?
【发布时间】:2022-07-22 01:33:04
【问题描述】:

为什么 cat1.set_size() 函数返回 None 而不是 "small"cat2.color 函数返回 "<__ main __. tiger object at>" 而不是 "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

【问题讨论】:

  • (1) 因为你没有返回任何东西。如果您希望它返回新大小,请添加return self.size。 (2) 因为需要将self 传递给super().__init__ 函数。
  • set_size 没有理由争论,因为你从不看争论。 Tiger.set_size也没有任何理由打电话给super().set_size
  • cat1_size != cat1.size
  • super().__init__(color, cat_type) 必须是 super().__init__(self, color, cat_type)
  • 该函数不包含任何return 语句。你为什么期望它返回任何东西?

标签: python class


【解决方案1】:

这是一个更好的组织。

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

【讨论】:

    【解决方案2】:

    它从不打印任何东西,因为你从不从方法返回任何东西。您需要在方法结束时返回所需的任何内容。

    return self.size
    

    在 set_size 方法结束时。

    【讨论】:

      【解决方案3】:

      如果方法中没有返回,它总是返回 None。

      【讨论】:

        【解决方案4】:

        这里有两个问题:

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

        2. 您正在重新运行Tigerset_size 中的__init__ 函数

        以下是更正后的代码:

        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)
        

        【讨论】:

          【解决方案5】:

          此代码稍作修改。

          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))
          

          【讨论】:

            猜你喜欢
            • 2015-03-13
            • 1970-01-01
            • 1970-01-01
            • 2017-07-16
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多