Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><code>isinstance</code>, since Python 2.6, has become quite nice as long as you follow the "key rule of good design" as explained in the classic "gang of 4" book: <strong>design to an interface, not to an implementation</strong>. Specifically, 2.6's new Abstract Base Classes are the only things you should be using for <code>isinstance</code> and <code>issubclass</code> checks, <strong>not</strong> concrete "implementation" types.</p> <p>Unfortunately there is no abstract class in 2.6's standard library to summarize the concept of "this number is Integral", but you can make one such ABC by checking whether the class has a special method <code>__index__</code> (<strong>don't</strong> use <code>__int__</code>, which is also supplied by such definitely non-integral classes as <code>float</code> and <code>str</code> -- <code>__index__</code> was introduced specifically to assert "instances of this class can be made into integers with no loss of important information") and use <code>isinstance</code> on that "interface" (abstract base class) rather than the specific implementation <code>int</code>, which is way too restrictive.</p> <p>You could also make an ABC summarizing the concept of "having m, h and s attributes" (might be useful to accept attribute synonyms so as to tolerate a <code>datetime.time</code> or maybe <code>timedelta</code> instance, for example -- not sure whether you're representing an instant or a lapse of time with your <code>MyTime</code> class, the name suggests the former but the existence of addition suggests the latter), again to avoid the very restrictive implications of <code>isinstance</code> with a <em>concrete implementation</em> cass.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload