duck typing assert
Steven D'Aprano
steve+comp.lang.python at pearwood.info
Thu Nov 8 18:33:01 EST 2012
On Thu, 08 Nov 2012 20:34:58 +0300, 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)
How very cute. And I don't mean that in a good way.
Why is this a class with a method, instead of a function that takes two
class arguments (plus any optional arguments needed)?
looks_like(Foo, IFoo)
is less "cute", reads better to English speakers, and much more Pythonic.
This isn't Java, not everything needs to be a class.
> 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
I don't understand the emphasis on assert for this code. It is enough
that looks() return a flag. The caller can then use that as an assertion:
assert looks(Spam).like(Ham)
or as a conditional:
if looks(food).like(Meat):
...
else:
...
Assertions are only one use for this check, and in my opinion, the least
useful one.
And why the warnings? In my opinion, using the warning mechanism as a way
to communicate the differences between the classes is an abuse of
warnings: they're not *warnings*, they are *diagnostic information*.
It is also fragile: the caller may have filtered warnings, and will not
see the messages you generate.
Lastly, I do not understand the purpose of this "wheezy.core" package.
Does it have something to do with Ubuntu Wheezy? The documentation is
unclear -- it refers to it as a "Python package" that "provides core
features", but doesn't say what the purpose of the package is: core
features of *what*? And the examples.rst file doesn't show any examples.
https://bitbucket.org/akorn/wheezy.core/src/ca5b902e9605/doc/examples.rst
--
Steven
More information about the Python-list
mailing list