DEV Community

loading...

Discussion on: Practical Python - Duck-typing to the rescue!

Collapse
arkocal profile image
Ali Rasim Koçal

Nice hack! But here is some little critique because the title implies a broader use for it.

Duct typing is awesome, but some Duck objects are dangerous, because

  1. They do not document themselves. In this case, it is OK because the variable names do it, but I would suggest not to use this approach if it obscures the intent of the object. To quote The Zen of Python (import this) explicit is better than implicit. Python avoids strong typing because it is mostly practical to allow anything that walks like a Duck, but the Duck itself should be implemented at some point, so the reader knows what it is about.
  2. A typo in argument name can lead to a long nasty debugging session. I am not sure whether that would work in your case, but a NamedTuple from the collections module is often a good fit for such use.
Collapse
alvarocavalcanti profile image
Alvaro Cavalcanti Author

You points are much appreciated, Ali! Thanks!

I do understand that the title is generic while the content only covers a smaller ground. I'm sorry for that. And to be fair, readability is a first-class concern of mine (I have a much over due unpublished article on the matter), thus the context-through-variable-name.

Also, I thought about using the NamedTuple but wasn't sure about the . accessor (and apparently was too lazy to search) which led me to the duck.