So Python's core language and built-ins use a lot of duck typing. But for our own code, say I want to create my own class with a method that deals with "certain types" of objects, is it more idiomatic to use duck typing:
class MerkleTree:
def method(self, document):
try:
hash_ = document.sha256_hash()
except AttributeError:
raise TypeError()
do_smth_with_hash(hash_)
or is it more idiomatic to just use a normal type check:
class MerkleTree:
def method(self, document):
if isinstance(document, SHA256Hashable):
raise TypeError()
hash_ = document.sha256_hash()
do_smth_with_hash(hash_)