11

这是一些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 中可能没有鸭子类型。维基百科有代码,但我看不懂。

4

3 回答 3

41

该代码并未显示整个故事。鸭子打字是关于尝试一些事情并在发生异常时处理它们。只要它嘎嘎叫,就把它当作鸭子对待,否则,就另当别论。

try:
    dog.quack()
except AttributeError:
    dog.woof()

这种行为在维基百科 Duck_typing 文章的顶部在对非鸭子类型语言的描述之后进行了解释:

在鸭子类型语言中,等效函数将获取任何类型的对象并调用该对象的 walk 和 quack 方法。如果对象没有被调用的方法,则该函数会发出运行时错误信号。如果对象确实有方法,那么无论对象的类型如何,它们都会被执行,从而唤起引号,从而产生这种类型的名称。

对于您的示例:

class Person:
    def help(self):
        print("Heeeelp!")

class Duck:
    def help(self):
        print("Quaaaaaack!")

class SomethingElse:
    pass

def InTheForest(x):
    x.help()

donald = Duck()
john = Person()
who = SomethingElse()

for thing in [donald, john, who]:
    try:
        InTheForest(thing)
    except AttributeError:
        print 'Meeowww!'

输出:

Quaaaaaack!
Heeeelp!
Meeowww!
于 2013-06-10T16:55:10.563 回答
5

是的,这是鸭子类型,Python 代码可以(并且经常)使用它。

http://en.wikipedia.org/wiki/Duck_typing#In_Python

在页面上方有一个更完整的 Python 示例:

class Duck:
    def quack(self):
        print("Quaaaaaack!")
    def feathers(self):
        print("The duck has white and gray feathers.")

class Person:
    def quack(self):
        print("The person imitates a duck.")
    def feathers(self):
        print("The person takes a feather from the ground and shows it.")
    def name(self):
        print("John Smith")

def in_the_forest(duck):
    duck.quack()
    duck.feathers()

def game():
    donald = Duck()
    john = Person()
    in_the_forest(donald)
    in_the_forest(john)

game()
于 2013-06-10T16:41:58.937 回答
0

当您在 Python 中定义一个方法时,您必须提供它所应用的对象,在您的情况下,它是self.

因此,您必须使用以下行调整您的代码以获得预期的行为:

class Duck:
    def help(self):
        print("Quaaaaaack!")

class Person:
    def help(self):
        print("Heeeelp!")
于 2013-06-10T16:43:13.223 回答