以下WikipediaからダックタイピングのRubyでの単純な例を引用
Ruby
def test(foo) puts foo.sound end class Duck def sound 'quack' end end class Cat def sound 'myaa' end end test(Duck.new) test(Cat.new)
結果
quack myaa
で、これをPythonでやってみた
Python
def test(foo): print foo.sound() class Duck: def sound(self): return "quack" class Cat: def sound(self): return "myaa" test(Duck()) test(Cat())
結果
quack myaa
もひとつおまけに
def test2(foo): print foo.sound class Duck2: def __init__(self): self.sound = "quack" class Cat2: def __init__(self): self.sound = "myaa" test2(Duck2()) test2(Cat2())
結果
quack myaa
これでよいのかな?
そういえば、あまり縁がなかったけどRubyもいいかも知れないなぁ。