Propose slight change to tutorial glossary entry on Duck Typing

P

Paddy

The official glossary entry here: http://docs.python.org/tut/node18.html#l2h-46
says:
"
duck-typing
Pythonic programming style that determines an object's type by
inspection of its method or attribute signature rather than by
explicit relationship to some type object ("If it looks like a duck
and quacks like a duck, it must be a duck.") By emphasizing interfaces
rather than specific types, well-designed code improves its
flexibility by allowing polymorphic substitution. Duck-typing avoids
tests using type() or isinstance(). Instead, it typically employs
hasattr() tests or EAFP programming.
"

I think it is should be changed to delete the use of hasattr and just
rely on EAFP as in the second example here:
http://en.wikipedia.org/wiki/Python_syntax_and_semantics#Exceptions

The text would then read:
"
duck-typing
Pythonic programming style that determines an object's type by
inspection of its method or attribute signature rather than by
explicit relationship to some type object ("If it looks like a duck
and quacks like a duck, it must be a duck.") By emphasizing interfaces
rather than specific types, well-designed code improves its
flexibility by allowing polymorphic substitution. Duck-typing avoids
tests using type(), hasattr() or isinstance(). Instead, it typically
employs an EAFP style of programming.
"

The reason is that a hasattr test only tests for an attribute name. If
it is present and say the method signature is wrong, then its the
excecution of the code using the attribute that will find that out so
it is redundant. Best to use EAFP for Duck typing as we trust you know
what it is you are doing.

- Paddy.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,602
Members
45,182
Latest member
BettinaPol

Latest Threads

Top