在父类内部使用'super()',为什么呢?

时间:2019-05-07 08:00:36

标签: python oop inheritance

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

2 个答案:

答案 0 :(得分:2)

BahdanauAttention继承自tf.keras.Model,这意味着其super中的init调用实际上调用了init的{​​{1}}方法。 / p>

还有一件事: 从Python 3开始,您无需将任何参数传递给tf.keras.Model调用:

super

答案 1 :(得分:0)

我建议您阅读有关Python 3中的foreach ($tmp as $key => $value) { mb_internal_encoding('UTF-8'); $pair = mb_strtolower($value['pair']); $value['pair'] = $pair; $value['position'] = $postions[$value['position_id']]; unset($value['position_id']); $result[$pair] = $value; } header('Content-Type: application/json'); echo json_encode($result); 方法的文章,这很棘手,特别是对于多重继承(即使不是继承),How does Python's super() work with multiple inheritance?