[Python-Dev] Duck-typing self

Steven Bethard steven.bethard at gmail.com
Thu Feb 19 00:30:14 CET 2009


On Wed, Feb 18, 2009 at 2:32 PM, Sebastian Rittau
<srittau at jroger.in-berlin.de> wrote:
> Hi!
>
> I am curious why the following will not work in Python:
>
>  class foo(object):
>      def bar(self):
>          print self.attr
>
>  class duck(object):
>      attr = 3.14
>
>  foo.bar(duck())
>
> Is it a design decision that duck-typing self does not work or is there a
> technical reason? From a practical standpoint it seems that being able to
> duck-type self has merit, for example in unit testing complex classes.

Works for me in 3.0:

Python 3.1a0 (py3k:69082, Jan 28 2009, 19:22:10) [MSC v.1500 32 bit
(Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> class Foo(object):
...     def bar(self):
...         print(self.attr)
...
>>> class Duck(object):
...     attr = 3.14
...
>>> Foo.bar(Duck())
3.14

Steve
-- 
I'm not *in*-sane. Indeed, I am so far *out* of sane that you appear a
tiny blip on the distant coast of sanity.
        --- Bucky Katt, Get Fuzzy


More information about the Python-Dev mailing list