【问题标题】:Can we use super method in a newly created class without any inheritance?我们可以在没有任何继承的情况下在新创建的类中使用超级方法吗?
【发布时间】:2021-11-18 19:56:51
【问题描述】:

我是 python 类的新手。我搜索并浏览了很多关于 super() 方法的文章,这造成了很多混乱。

类型 - 1

class Square():
    def __init__(self,side):
        self.side = side
        print("Its confusing")
        super().__init__()
  
    def area(self):
        return self.side * self.side
    
c = Square()
print(c.area())

Type - 1 中,如果我提到super().__init__() 有什么用呢?我不明白为什么我们在一个新创建的类中使用super().__init__(),它不是从任何其他类继承的。而且我不明白它在这里做了什么。

如果我们在分配self.side = side 之后给它,它会正常运行并被执行。

类型 - 2

class Square():
    def __init__(self):
        # self.side = side
        # print("Its confusing")
        super().__init__()
e = Square()
e()

如果我只在__init__ 内给出super().__init__(),它会给出错误。 TypeError: 'Square' object is not callable

我的疑问是:

1.我们可以在新创建的类中使用super().__init__() 吗?它运行良好,没有错误。

2.如果我只将super().__init__() 放在__init__ 中,为什么Type - 2 会抛出错误?

你能用简单的话说一下吗?

【问题讨论】:

  • 继承自object
  • 在你的文本中使用`super().init()`你的意思是super().__init__()?
  • "every" 类是object 的子类,尝试打印issubclass(Square, object)。通常它是多余的并省略它
  • 您的type-2 代码只是一个错字。你有e()。当您的类不可调用时,为什么要故意导致错误?
  • 你应该看看answers这个问题。 super().__init__() 在多重继承中非常有用。

标签: python inheritance single-inheritance


【解决方案1】:

对于type-1,因为 Square 类不继承自任何基类,所以对 super().__init__() 的调用实际上什么都不做。

但是,保留此代码仍然很好,以防您使用 Square 类进行多重继承。在另一个问题中有一个示例,请参见此处: https://stackoverflow.com/a/8613067/362792。 所以复制那个例子,假设你有类 Square、类 Color,然后是类 ColoredSquare:

class ColoredSquare(Square, Color):
   ...

在这种情况下,当创建 ColoredSquare 实例时,来自 Square 的 super().__init__() 实际上是在调用 Color.__init__() 方法。所以确实有必要。


对于type-2,您的错误不是来自 init 方法。相反,它实际上是 e() 正在为“TypeError: 'Square' object is not callable”产生错误。

要以这种方式使用对象,您需要有调用方法。 例如。

class Square():
    def __init__(self):
        super().__init__()

    def __call__(self):
        print('call for square')


e = Square()
e()

【讨论】:

    【解决方案2】:

    继承用于回收代码......并且您使用super“fetch”代码在父类中定义。

    # parent class
    class Parallelogram:
        def __init__(self, side1, side2):
            self.side1, self.side2 = side1, side2
    
        def area(self):
            return self.side1 * self.side2
    
    
    # child class
    class Square(Parallelogram):
        def __init__(self, side):
            super().__init__(side, side)
    
    
    p = Parallelogram(2, 3)
    print(p.area())
    # 6
    
    s = Square(4)
    print(s.area())
    # 16
    
    print(issubclass(Parallelogram, object))
    #True
    print(issubclass(Square, Parallelogram))
    #True
    
    print(hasattr(Square, 'area')) # inherited from the parent 
    True
    

    【讨论】:

    • btw其不幸的是,您将Square class转发到其基类的area() method。在这种情况下,area() 方法是多余的,可以省略。这可能被误解为在派生类中实现方法的正确方法。 span>
    • @ Quamrana也是在Python2.7中的? span>
    • 什么是it你是指什么?当然@ 987654327在Python 2中的不同方式工作。 span>
    • 抱歉,我的意思是如果也在Python2.7中从平方声明区域,如冗余 span>
    • 是的,python 2/3 在这方面没有改变。在大多数OOP语言中都是一样的。继承的整个点是能够在没有程序员对派生类进行任何编码的情况下继承所有父母的方法。 span>
    猜你喜欢
    • 2016-03-06
    • 1970-01-01
    • 2017-05-09
    • 1970-01-01
    • 2014-08-18
    • 1970-01-01
    • 2014-05-17
    • 2015-01-08
    • 1970-01-01
    相关资源
    最近更新 更多