duck typing assert‏

Steven D'Aprano steve+comp.lang.python at pearwood.info
Fri Nov 9 00:40:19 CET 2012


On Thu, 08 Nov 2012 15:39:24 -0500, Terry Reedy wrote:

[...]
> 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.

Properties and methods do not have the same interface:

IFoo.bar  # returns a computed property
Foo.bar()  # calls a method

Since the interfaces are different, duck-typing will fail. It will 
actually fail in a potentially nasty way:

x = Foo.bar  # doesn't raise an exception, gives the method object
# ... much later
do_something_with(x)  # blows up potentially far, far away




-- 
Steven


More information about the Python-list mailing list