Is this duck-typing in Python?

前端 未结 3 895
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-14 12:15

Here is some Ruby code:

class Duck
  def help
    puts \"Quaaaaaack!\"
  end
end

class Person
  def help
    puts \"Heeeelp!\"
  end
end

def InTheForest x
         


        
3条回答
  •  既然无缘
    2020-12-14 12:46

    The code does not show the whole story. Duck typing is about trying something and handling exceptions if they occur. As long it quacks, treat it like a duck, otherwise, treat it differently.

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

    This behavior is explained at the top of the wikipedia Duck_typing article following a description of a non-duck-typed language:

    In a duck-typed language, the equivalent function would take an object of any type and call that object's walk and quack methods. If the object does not have the methods that are called then the function signals a run-time error. If the object does have the methods, then they are executed no matter the type of the object, evoking the quotation and hence the name of this form of typing.

    For your example:

    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!'
    

    output:

    Quaaaaaack!
    Heeeelp!
    Meeowww!
    

提交回复
热议问题