Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #32979
| From | Terry Reedy <tjreedy@udel.edu> |
|---|---|
| Subject | Re: duck typing assert |
| Date | 2012-11-08 15:39 -0500 |
| References | <DUB117-W18E06D840F2407846F709191690@phx.gbl> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.3464.1352407186.27098.python-list@python.org> (permalink) |
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
Back to comp.lang.python | Previous | Next — Next in thread | Find similar | Unroll thread
Re: duck typing assert Terry Reedy <tjreedy@udel.edu> - 2012-11-08 15:39 -0500
Re: duck typing assert Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-11-08 23:40 +0000
Re: duck typing assert Terry Reedy <tjreedy@udel.edu> - 2012-11-08 23:44 -0500
Re: duck typing assert Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-11-09 06:30 +0000
Re: duck typing assert Terry Reedy <tjreedy@udel.edu> - 2012-11-09 12:03 -0500
csiph-web