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 1 year has passed since last update.

[py2rb] @property

Last updated at Posted at 2021-12-13

はじめに

移植やってます

@property (Python)

class IndexedReaderMixin(NoOpBaseReader):
    @property
    def index(self):
        return self._offset_index

Pythonのデコレーターは、関数に機能を追加するための仕組み。
ややこしい仕組みですが、@propertyは比較的簡単で、所謂セッターゲッターの様なもの。

attr_reader (Ruby)

class A
  attr_reader :index
  
  def initialize
    @index = 0
  end

  def add
    @index += 1
  end
end

a = A.new
puts a.index
puts a.add
puts a.index += 1 # error

皆さん大好きattr_reader

module A
  def reset
    @index = 0
  end

  def add(value)
    @index += value
  end
end

class B
  include A
end
a = B.new
puts a.reset
puts a.add(2)
puts a.index # error

ちょっと格好悪いですが、セッターゲッター入れましょうか。

メモ

  • Python の @property を学習した
  • 道のりは遠そう
0
0
4

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?