Reference

Marko Rauhamaa marko at pacujo.net
Wed Mar 5 07:23:21 CET 2014


Rustom Mody <rustompmody at gmail.com>:

> * ... which summarizes my objection in this thread: Python's 'is'
> leaks the machine abstraction. 'id' does it legitimately (somewhat),
> 'is' does it illegitimately

I agree that the Python data model can be exceedingly challenging to a
beginner. However, I wouldn't throw the baby away with the bathwater,
but look for ingenious ways to teach it.

The Spanish say, "Mal de muchos, consuelo de tontos." Python is hardly
alone in this mess. For example, Java's '==' operator corresponds to
Python's "is". Nobody would consider throwing Java's "==" away. Instead,
we are offered esoteric rules like:

   If the value p being boxed is true, false, a byte, a char in the
   range \u0000 to \u007f, or an int or short number between -128 and
   127, then let r1 and r2 be the results of any two boxing conversions
   of p. It is always the case that r1 == r2. <URL: http://docs.ora
   cle.com/javase/specs/jls/se5.0/html/conversions.html#5.1.7>

For Python's "==", Java offers the "equals()" method. So is it wrong in
Java to check:

   if (list.equals(null)) {

The spec says:

   For any non-null reference value x, x.equals(null) should return
   false. <URL: http://docs.oracle.com/javase/7/docs/api/java/lang/O
   bject.html#equals%28java.lang.Object%29>

So it *should* work, but why would you avoid the more natural test:

   if (list == null) {


Similarly, in Python:

   if the_list == None:

*should* work (even if there's no such stipulation in Python's reference
 material), but why wouldn't you use the more natural:

   if the_list is None:


Marko



More information about the Python-list mailing list