On Wed, 21 Apr 2021 12:36:34 -0700 Christopher Barker <pythonchb@gmail.com> wrote:
But that's not what duck typing is (at least to me :-) ) For a given function, I need the passed in object to quack (and yes, I need that quack to sound like a duck) -- but I usually don't care whether that object waddles like a duck.
So yes, isinstance(obj, Sequence) is really the only way to know that obj is a Sequence in every important way -- but if you only need it to do one or two things like a Sequence, then you don't care.
It depends on the context, though. Sometimes it's better to check explicitly and raise a nice error message, then raise a cryptic error much further that seems to bear little relationship to the line of code the user wrote. Especially if that error is raised at the end of a 10-minute computation, or after sending 1GB of data to a S3 bucket. For this reason, when there's no use case for accepting many kinds of sequences in a user-facing API, I find it useful to do a `isinstance(x, (list, tuple))` check before proceeding. Yes, it's not pure duck typing, but who cares? Regards Antoine.