0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

はじめに

移植やってます

init (Python)

class IteratorContextManager(NoOpBaseReader):
    def __init__(self, *args, **kwargs):
        self._func = kwargs.pop('parser_func')
        self._args = args
        self._kwargs = kwargs
        if type(self) == IteratorContextManager:
            self.reset()
        super(IteratorContextManager, self).__init__(*args, **kwargs)

インスタンスを生成されるときに呼び出されます。
しかし、super.__init__ってなかなか凄いですね。

initialize (Ruby)

class A
  def initialize(*args)
    puts "A initialize"
    __init__(args)
  end

  def __init__(*args)
    puts "A __init__" + args.to_s
  end
end

class B < A
  def initialize(*args)
    puts "B initialize"
    __init__(args)
  end

  def __init__(*args)
    puts "B __init__" + args.to_s
    super()
    super
  end
end

b = B.new("instance")
b.__init__("method")

# B initialize
# B __init__[["instance"]]
# A __init__[]
# A __init__[["instance"]]
# B __init__["method"]    
# A __init__[]
# A __init__["method"]   

initializeをprivateからpublicにしようかなとも思いましたが、__init__メソッドを用意して呼び出す形にする予定。

class IteratorContextManager < NoOpBaseReader
  def initialize(*args, **kwargs)
    __init__(args, kwargs)
  end

  def __init__(*args, **kwargs)
    @_func = kwargs.delete('parser_func')
    @_args = args
    @_kwargs = kwargs
    reset() if self.class == IteratorContextManager
    super
  end
end

単一継承かつ引数が同じなので、superでよさそう。

メモ

  • Python の __init__ を学習した
  • 道のりは遠そう
0
0
0

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?