LoginSignup
0
0

More than 1 year has passed since last update.

[py2rb] 多重継承

Last updated at Posted at 2021-12-13

はじめに

移植やってます

多重継承 (Python)

class NoOpBaseReader(object):
    def __init__(self, *args, **kwargs):
        pass

class IndexedReaderMixin(NoOpBaseReader):

class IndexedXML(IndexedReaderMixin, XML):

このIndexedReaderMixinが次に継承される際多重継承となるため、Mixin化を図りたいと思います。
親クラスのインスタンス化でpassしているので、弊害は少ないと思いますが。

コメント欄にて教えていただきましたが、多重継承といいましても、単一継承を何回も繰り返しているだけのようです。ダイヤモンド継承になっていてもうまく検索できる仕組みのようです。

[追記]
いつも参照させてもらっている、お気楽プログラミングさんに分かりやすい解説がありました。
お気楽 Python3 プログラミング超入門

module (Ruby)

module A
  def __init__(*args)
    puts "module A"
  end
end

class B
  include A
  def __init__(*args)
    puts "class B"
    super
  end
end

p B.ancestors
b = B.new.__init__

# [B, A, Object, Kernel, BasicObject]
# class B 
# module A

Rubyにとって__init__メソッドは特殊なメソッドではないので、継承チェーンに気を付ければ、問題ないと思います。
ということは、ちょっと格好悪くても、継承を繰り返せば良さそうです。
@shiracamus さん、ありがとうございました。

メモ

  • Python の 多重継承 を学習した
  • 道のりは遠そう
0
0
3

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0