Chris's Wiki :: blog/python/GettingInterfaces Commentshttps://utcc.utoronto.ca/~cks/space/blog/python/GettingInterfaces?atomcommentsDWiki2009-11-20T21:14:29ZRecent comments in Chris's Wiki :: blog/python/GettingInterfaces.From 64.101.44.136 on /blog/python/GettingInterfacestag:CSpace:blog/python/GettingInterfaces:78e9a4431f0444aef2b36d1d1a1915a3546ca7d4From 64.101.44.136<div class="wikitext"><p>"
me (brendan)-> If an class is string compatible, it will descend from basestring.</p>
<p>someone else's reply->
I disagree very strongly. The essence of duck typing is that you can be completely compatible with X without anywhere near X's inheritance tree.
"</p>
<p>Yes... that is the "essence" of duck typing, but that's not how python does it... see basestring's documentation.</p>
<p>The post's author is talking about violating duck typing, so I was showing him how to do so in a minimalistic way.</p>
<p>His other idea of making all types implement interfaces, and doing explicit checks for interfaces would kill duck typing altogether.</p>
<p>Numbers and strings in python are already special and have abstract base classes designed for isinstance checks.</p>
<p><a href="http://docs.python.org/library/numbers.html">http://docs.python.org/library/numbers.html</a>
<a href="http://docs.python.org/library/functions.html">http://docs.python.org/library/functions.html</a> see basestring here</p>
<p>Python supports duck typing, but it doesn't <em>force</em> you to use it. In practice, it's sometimes useful to do an explicit type check against strings or numbers in order to provide a simulation of function overloading.</p>
<p>In particular, I've seen a lot of libraries that offer functions that except either an object, or a string that can be used to <em>construct</em> a compatible object, as in my earlier example.</p>
<p>NOTE: earlier I constructred a regex with an argument of "obj" whereas that was a typo, and I meant to type "regex".</p>
</div>2009-11-20T21:14:29ZBy Chris Siebenmann on /blog/python/GettingInterfacestag:CSpace:blog/python/GettingInterfaces:0c777d54ca4d0e477a6383ac8a639c86fc26d4ecChris Siebenmann<div class="wikitext"><p>I agree that <code>hasinterface()</code> is the better name. (I am not good at
names for things.)</p>
</div>2009-11-19T04:40:42ZFrom 78.35.25.18 on /blog/python/GettingInterfacestag:CSpace:blog/python/GettingInterfaces:1065c5fd3aaf5627aa0299e55cc24b069e05bc52From 78.35.25.18<div class="wikitext"><p>Shouldn’t that be <code>hasinterface</code>? (If not <code>supportsinterface</code>… but no one wants to type that much.)</p>
<p>—<a href="http://plasmasturm.org/">Aristotle Pagaltzis</a></p>
</div>2009-11-18T23:13:57ZBy Chris Siebenmann on /blog/python/GettingInterfacestag:CSpace:blog/python/GettingInterfaces:a8d86efa5db64173f6061dfbdd6404bd0a3abbbfChris Siebenmann<div class="wikitext"><blockquote><p>If an class is string compatible, it will descend from basestring.</p>
</blockquote>
<p>I disagree very strongly. The essence of duck typing is that you can
be completely compatible with X without anywhere near X's inheritance
tree. If we are taking it seriously, we have to admit that you can be
a string without being a <code>basestring</code>.</p>
<p>(<code>basestring</code> is actually an unusual case, because it is an abstract
type with no behavior and is exposed by the C API, so you can actually
write C-level extensions that inherit from it but completely do their
own thing. By no means are all Python types this convenient.)</p>
</div>2009-11-18T21:40:56ZFrom 64.101.44.136 on /blog/python/GettingInterfacestag:CSpace:blog/python/GettingInterfaces:5d6eb32cd3ace72fb425155566f1acb330ed624cFrom 64.101.44.136<div class="wikitext"><p>You are way over thinking this. Your original problem was that you wanted to check whether an object is a string or a regular expression.</p>
<p>def myfunc(regex):</p>
<pre>
if isinstance(regex, basestring):
regex = re.compile(obj)
return re.match("some string")
</pre>
<p>Done!</p>
<p>This overloads on string, and assumes any other kind of object passed in is duck type compatible with the results of re.compile(obj).</p>
<p>Also, worrying about whether two different match functions are going to be semantically compatible is silly. If an class is string compatible, it will descend from basestring. If it doesn't, then it's a bug on the string implementation, not your code. If the caller passes in something weird that is neither a string nor a regex, then it is a bug in the caller.</p>
</div>2009-11-18T20:17:42Z