-
Website
http://www.dobesland.com -
Original page
http://www.dobesland.com/2007/10/07/python-isinstance-considered-useful/ -
Subscribe
All Comments -
Community
-
Top Commenters
-
spangen
1 comment · 1 points
-
Janette Toral
1 comment · 11 points
-
Targe
1 comment · 1 points
-
dobes_vandermeer
17 comments · 3 points
-
sujitkale
1 comment · 1 points
-
-
Popular Threads
" You should instead infer what type of object you have by checking for the presence or absence of particular methods".
That isn't quite right. You should just go ahead and use whatever object you have assuming it is the correct object and trust Pythons run-time type checking and your programming team to "do the right thing". It isn't that explicit interface checking isn't needed in Python - its just not the only way, or necessarily the first way to try when programming Python.
- Paddy.
Ref: http://en.wikipedia.org/wiki/Duck_typing
The main point of the article is not to decry duck typing, but instead to defend isinstance() as something which is not harmful, but useful.
I've heard the argument that checking the types of things shows a problem with the design of your code, and you should never use hasattr or isinstance. Maybe so - but most of the suggested solutions wouldn't work for objects I didn't create, or require a lot of extra unnecessary code.
Recently I've been introduced into PyProtocols, which seems to provide an elegant solution to the problems with both interfaces (hard to adapt other people's object to an interface) and duck typing (fragility in the face of changes).