这是一些Ruby代码:
class Duck
def help
puts "Quaaaaaack!"
end
end
class Person
def help
puts "Heeeelp!"
end
end
def InTheForest x
x.help
end
donald = Duck.new
john = Person.new
print "Donald in the forest: "
InTheForest donald
print "John in the forest: "
InTheForest john
而且,我将它翻译成 Python:
import sys
class Duck:
def help():
print("Quaaaaaack!")
class Person:
def help():
print("Heeeelp!")
def InTheForest(x):
x.help()
donald = Duck()
john = Person()
sys.stdout.write("Donald in the forest: ")
InTheForest(donald)
sys.stdout.write("John in the forest: ")
InTheForest(john)
结果是一样的。这是否意味着我的 Python 代码正在使用鸭子类型?我找不到鸭子类型的例子,所以我认为 Python 中可能没有鸭子类型。维基百科有代码,但我看不懂。