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
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.
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.
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.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
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
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.NamedTuple
from thecollections
module is often a good fit for such use.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.