11

Here is some Ruby code:

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

And, I translated it to 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)

The result is the same. Does this mean my Python code is using duck-typing? I couldn't find a duck-typing example, so I thought there may be no duck-typing in Python. There is code in Wikipedia, but I couldn't understand it.

sawa
  • 165,429
  • 45
  • 277
  • 381
a1204773
  • 6,923
  • 20
  • 64
  • 94

3 Answers3

42

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!
Ahsanul Haque
  • 10,676
  • 4
  • 41
  • 57
dansalmo
  • 11,506
  • 5
  • 58
  • 53
5

Yes, this is duck typing, which Python code can (and often does) use.

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

Further up on the page there is a more complete example in 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()
Andrew Clark
  • 202,379
  • 35
  • 273
  • 306
0

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!")
the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Raphael Pr
  • 896
  • 10
  • 28
  • 1
    True, but not an answer. Plus, as OP says "the result is the same" I assume the error is only present in the code in the question, not in OP's actual code. –  Jun 10 '13 at 16:45
  • I understand how it works, I just couldnt find example in python using duck typing... I searched in google but couldnt find result then i tried to translate ruby to python and it worked.. I was just curious what I wrote is duck typing or not. – a1204773 Jun 10 '13 at 16:49