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.

Python to RubyAdvent Calendar 2021

Day 7

[py2rb] getattr

Last updated at Posted at 2021-12-09

はじめに

移植やってます

getattr (Python)

class a:
    def pg(self):
        print("print getattr")

getattr(a(), "pg")()

インスタンスメソッドを呼び出しています。

def _accession(self, key):
    return getattr(key, 'accession', None)

これは、引数が3つですね。

respond_to? (Ruby)

class A
  def accession
    "A"
  end
end

class B; end

a = A.new
b = B.new

puts a.respond_to?(:accession) ? a.accession : nil
puts b.respond_to?(:accession) ? b.accession : nil

@Nabetani さんの コメント を活用させていただきました。

メモ

  • Python の getattr を学習した
  • 道のりは遠そう
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?