More random python observations from a perl programmer

Jeremy Hylton jeremy at
Thu Aug 19 13:14:09 EDT 1999

[Tom Christiansen <tchrist at> writes:]
>Well, it's the polymorphism thing.  If "+" is supposed to work on anything
>(which I don't agree with, but that's not my decision), I don't understand
>why "in" shouldn't.  I keep thinking that everything should work on

I kind of agree with you.  I often use in with a dictionary without
thinking about it and then go back to fix it.  (I'm quite happy that +
works on many types BTW.)

It is interesting to note that early version of JPython implemented in
for dictionaries, based on exactly the reasoning you describe.  It
ended up leading to some puzzling behavior when used in a for loop.
I'm hazy on the detail, but I think the exact behavior was: JPython
would treat a dictionary as a sequence within a loop context provided
that there was a 0 key (plus zero or more successive integers).  I
passed a dictionary without a 0 key; JPython caught an exception and
treated the dictionary as an empty sequence.  I hadn't intended to use 
a dictionary in the for loop, rather a function had mistakenly
returned a dictionary rather than the dictionary's keys.  

I'm not sure what the moral of the story is, except that JPython
doesn't implement in for dictionaries anymore and I'm mostly happy
about that.

>everything.  For example, this surprises me:
>    x = {1:2, 3:4}
>    y = {10:20, 30:40}
>    z = x + y
>The latter isn't legal.  But in Perl, you can merge hashes easily.
>    %x = (1,2,3,4);
>    %y = (10,20,30,40);
>    %z = (%x, %y);
>    print $z{1}
>    2
>    print $z{30}
>    40

You can do almost exactly this in Python using the update method on

    x = {1:2, 3:4}
    y = {10:20, 30:40}
    z = {}
    print z
    {30: 40, 3: 4, 10: 20, 1: 2}

As you noted with the [].sort operation, you need to explicitly create 
the new object that call some methods on it.  In my book it is at
worst a minor annoyance.

>:Thanks for the document.  It was very interesting reading.
>You're welcome.  I got so fed up with the damned flames coming
>from people who don't actually even know what they're talking about,
>that I decided to make sure I wasn't one of them. :-)

Indeed, I enjoyed reading both your posts this morning.  While I don't 
agree with all of your criticisms, I think you make many good points.
I did notice that some of your criticisms were out-of-date, perhaps
reflecting Python previous to 1.5.2.  (And some of your criticisms are 
already being addressed, such as the string methods that are in the
current CVS tree but not yet release.
    x = 'asdbasdas'
    ['a', 'dba', 'da', '']
    # instead of:
    # import string
    # string.split(x, 's')


More information about the Python-list mailing list