My focus varies randomly between current events, fatherhood, navel-gazing, programming, research, and technology.

February 12, 2009

Python Strangeness Resolved

Yesterday, I posted about what seemed to be a Python inconsistency. Basically, we were puzzled as to why Python would allow:

l = [1, 2, 3]
t = (4, 5, 6)
l += (4,)

but would generate an error with:
l = [1, 2, 3]
t = (4, 5, 6)
l = l + (4,)

One of the former TAs for 6.00 pointed out that there are two parts to consider. The first is that we shouldn't assume __add__ (implements +) and __iadd__ (implements +=) have the same semantics. __iadd__ is supposed to actually modify the object in place. __add__ doesn't have that stipulation.

Python has a fall back behavior where if it can't find __iadd__ implemented for a particular object, then it will try to use __add__, however, for lists __iadd__ is overridden so that it functions like extend().

The second point to consider is in the following bit of code:
l = [1,2,3]
t = (4,5,6)
l = l + t

The expression l + t should evaluate to a value. What should the type be? tuple or list? It's ambiguous. Whereas:
l = [1,2,3]
t = (4,5,6)
l += t

In l += t, __iadd__ knows that it's modifying a list in place, so it can take appropriate action with a tuple argument. __iadd__ for tuples isn't implemented because tuples are immutable.

Dilemma resolved.

February 11, 2009

Python Strangeness

I'm one of the teaching assistants (TA) for 6.00 this semester.  Another TA sent this to our mailing list:

This is just for your amusement. I came across an interesting quirk of python.

First the normal stuff:
>>> [1,2,3] + [4]
[1,2,3,4]
>>> (1,2,3) + (4,)
(1,2,3,4)
>>> (1,2,3) + [4]
Traceback (most recent call last):
 File "", line 1, in 
  (1,2,3) + [4]
TypeError: can only concatenate tuple (not "list") to tuple

>>> [1,2,3] + (4,)
Traceback (most recent call last):
 File "", line 1, in 
  [1,2,3] + (4,)
TypeError: can only concatenate list (not "tuple") to list

>>> l = [1,2,3]
>>> l + (4,)
Traceback (most recent call last):
 File "", line 1, in 
  l + (4,)
TypeError: can only concatenate list (not "tuple") to list

So far so good, but now:
>>> l += (4,)
>>> l
[1, 2, 3, 4]

But it's not symmetric:
>>> l = (1,2,3)
>>> l += [4]
Traceback (most recent call last):
 File "", line 1, in 
  l += [4]
TypeError: can only concatenate tuple (not "list") to tuple

Which is to say, list's __iadd__ function is polymorphic to tuples, but
tuple's isn't to lists, and list's __add__ isn't either. Odd and a little
ugly, but yeah. Anyone know whether this is a bug or a feature?

February 02, 2009

Things I learned during my first 6 weeks of fatherhood

Cordelia Zorana turns 6 weeks old tomorrow, which is not some special number other than it's a number greater than 5 that is a multiple of 1, 2, and 3. It's also the first perfect number (h/t Ian). Natalija and I lucked out in many respects. Cordelia is perfectly healthy, not-too-fussy, and is already sleeping through the night - assuming, that is, she goes to sleep at the end of the evening.

That said, babies have no user manual, and this fact makes for some amusing learning experiences, mainly because they involve bodily fluids and solids.

  1. Babies use lots of diapers. They are amazing if only for the sheer volume of poop and pee they produce relative to their body size.
  2. There are few biological functions more awe-inspiring than a "meconium explosion."
  3. Never turn your back on an undiapered baby. They will use this opportunity to finish draining their bladder.
  4. A bath towel swaddling a baby waiting for a bath is not a substitute for a diaper. Avoid wearing nice clothes.
  5. Never turn your back on an undiapered baby. They will use this opportunity to finish emptying their bowel.
  6. You will be peed on.
  7. You will be pooped on.
  8. You will be puked on.
  9. The set of 6,7,8 is a power set. That is you may be peed on, you may be peed and pooped on, you may be peed and puked on, you may get the trifecta, etc.
  10. Nine times out of 10, the diaper will be wet when you stick a finger in to check. That 1 time out of 10 will teach you to peer down the backside first.
  11. Drool is part of the game. All those old t-shirts you failed to get rid of now have a new purpose.
  12. Baby heads are floppy. Fortunately, they're also bouncy, but don't press your luck.
  13. Mirrors fascinate the hell out of babies.