Is this duck-typing in Python?

前端 未结 3 894
佛祖请我去吃肉
佛祖请我去吃肉 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:34

    When you are defining a method in Python, you have to provide the object on which it applies, which, in your case, is self.

    Therefore you have to adapt your code with the following line to have the expected behaviour:

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

提交回复
热议问题