duck typing assert‏

Terry Reedy tjreedy at udel.edu
Thu Nov 8 15:39:24 EST 2012


On 11/8/2012 12:34 PM, Andriy Kornatskyy wrote:
>
> People who come from strongly typed languages that offer interfaces
> often are confused by lack of one in Python. Python, being dynamic
> typing programming language, follows duck typing principal. It can as
> simple as this:
>
> assert looks(Foo).like(IFoo)
>
> The post below shows how programmer can assert duck typing between
> two Python classes:
>
> http://mindref.blogspot.com/2012/11/python-duck-typing-assert.html
>
> Comments or suggestions are welcome.

 From the post:
'''
So far so good. Let fix it and take a look at properties:

from wheezy.core.introspection import looks

class IFoo(object):

     def foo(self, a, b=None):
         pass

     @property
     def bar(self):
         pass


class Foo(object):

     def foo(self, a, b=None):
         pass

     def bar(self):
         pass

assert looks(Foo).like(IFoo)

Here is output:

test.py:21: UserWarning: 'bar': is not property.
   assert looks(Foo).like(IFoo)
Traceback (most recent call last):
   File "test.py", line 21, in
     assert looks(Foo).like(IFoo)
AssertionError
'''

I view this check as an error. Properties are intended to be transparent 
to the user. One use of properties is to make something that is not a 
Mallard act like a Mallard. So this check breaks duck typing.

-- 
Terry Jan Reedy




More information about the Python-list mailing list