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] callable

Last updated at Posted at 2022-01-28

はじめに

移植やってます。
( from python 3.7 to ruby 2.7 )

callable (Python)

if callable(func):

こちらの記事が分かりやすい。独習Pythonには載っていない

defined? (Ruby)

def a; end
b = lambda { |x| p x }
c = 1
class D; end
e = D.new

p defined?(a)
p defined?(b)
p defined?(c)
p defined?(D)
p defined?(e)
p defined?(f)
p '----------'
p [a, b, c, D, e]
p [a.class, b.class, c.class, D.class, e.class]

# output
"method"
"local-variable"
"local-variable"
"constant"
"local-variable"
nil
"----------"
[nil, #<Proc:0x000002a58a4228e0 main.rb:2 (lambda)>, 1, D, #<D:0x000002a58a422840>]
[NilClass, Proc, Integer, Class, D]
Python Ruby
組み込み関数 True method
未定義の関数 False nil
定義済みの関数 True method
定義済みのクラス True constant
lambda - local-variable

行けそうです。

追記

list = [0, 1, 2, 3]
iter_list = iter(list)
print(callable(iter_list))

# false

iter()の場合、falseになります。

list = [0, 1, 2, 3].to_enum

p defined?(list)
p list.class

# "local-variable"
# Enumerator

よって、Enumetatorの場合、クラスを見る必要があります。

メモ

  • Python の callable を学習した
  • 百里を行く者は九十里を半ばとす
0
0
2

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?