title: Duck-typing user-defined classes
body: <p>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:</p>
  
  <pre class="lang-py prettyprint-override"><code>class MerkleTree:
  
      def method(self, document):
          try:
              hash_ = document.sha256_hash()
          except AttributeError:
              raise TypeError()
          do_smth_with_hash(hash_)
  </code></pre>
  
  <p>or is it more idiomatic to just use a normal type check:</p>
  
  <pre class="lang-py prettyprint-override"><code>class MerkleTree:
  
      def method(self, document):
          if isinstance(document, SHA256Hashable):
              raise TypeError()
          hash_ = document.sha256_hash()
          do_smth_with_hash(hash_)
  </code></pre>