[Tutor] Integer?

Luke Paireepinart rabidpoobear at gmail.com
Wed Dec 6 10:07:02 CET 2006

> Very interesting. But what is "duckly-typed"? I'm so dumb I can't 
> distinguish between a typo and a technical term..
> Dick Moores
Refer to the Wikipedia article on Duck Typing:

Basically, if you saw my example earlier of substituting stdout:
class FileLikeObject(object):
   def __init__(self,current_stdout):
       self.data = []
       self.oldstdout = current_stdout
       sys.stdout = self

   def write(self,arg):
       if arg != '\n':

   def output(self):
       sys.stdout = self.oldstdout
       print self.data
       sys.stdout = self

import sys
f = FileLikeObject(sys.stdout)
print "hello."
print "hi"

sys.stdout expects a file-like object, that has methods such as 'write'.
So I implemented a class with a 'write' method, and sys.stdout happily 
used it as an output stream.
It's not the same type of object as the original sys.stdout was.

 From IDLE,
 >>> print sys.stdout
<idlelib.rpc.RPCProxy instance at 0x00B403F0>

So you see idle itself has replaced the default console stream of
 >>> print sys.stdout
<open file '<stdout>', mode 'w' at 0x0097E068>

with its own version, which I replaced with my own version (the class 
instance with the write method).

All that matters is that the object has a 'write' method for it to be 
used as stdout (AFAIK).


More information about the Tutor mailing list