python - 在父类本身中使用了“super()”,为什么?
问题描述
我是 python 编程的新手,对 python 中的 oops 概念感到困惑。我知道super()
是用来使用父类中定义的方法,但是我不明白为什么父类本身使用super()
关键字。
class BahdanauAttention(tf.keras.Model):
def __init__(self, units):
super(BahdanauAttention, self).__init__()
self.W1 = tf.keras.layers.Dense(units)
self.W2 = tf.keras.layers.Dense(units)
self.V = tf.keras.layers.Dense(1)
解决方案
BahdanauAttention
继承自tf.keras.Model
,意味着其内部的调用super
实际上init
调用了 的init
方法tf.keras.Model
。
还有一件事:从 Python 3 开始,您不需要将任何参数传递给super
调用:
super().__init__()
推荐阅读
- smtp - 在 Julia 中使用 SMTPClient.jl 发送电子邮件:curl_easy_perform() failed: SSL peer certificate or SSH remote key was not OK
- c# - 如何使用它的指针直接从托管代码中的非托管读取矢量数据
- javascript - 尽管我正在对其进行变异,但对象返回相同
- scala - 如果日期为月底,则使用石英定义的下一个执行时间失败
- java - 如何反复检查用户是否在 Firebase 身份验证中得到验证?
- ios - 无法快速更改 ui 标签
- performance - 堆栈和堆性能?
- node.js - 如何自动将用户添加到公会?
- ios - 如何在 Swift 5 中将文档转换为自定义对象?
- c# - 无法在 Visual Studio 2019 中加载用户控件设计视图