Perl is worse! (was: Python is Wierd!)

Alex Martelli alex at magenta.com
Fri Jul 28 09:46:38 EDT 2000


"Steve Lamb" <grey at despair.rpglink.com> wrote in message
news:slrn8o24mr.e7.grey at teleute.rpglink.com...
    [snip]
> >Firstly, it's unnecessary.  If I have a single value in X, and I want to
> >make it a single-element list, there's a way to do that:
>
>     Really?
>
> >>>> some_variable = [X]
>
>     Seems counter intuative, doesn't it,

Doesn't seem counter-intuitive at all to me.  [x] makes a
single-element list, whatever x refers to is that single element.

> list() could take that single value
> and make it a single item in a list.  I mean, c'mon, why have list when
you
> could just construct a for loop to use an append method?

list is more compact and more efficient than an append loop. list(x) is
just as compact and effective as [x], but they do different things.


> >Secondly, it's counterintuitive.  You say "all of those can be converted
> >into lists"; I look at 1 and think, "what is the list equivalent of an
> >integer?"
>
>     That is because you're thinking in terms of types and not just data.
It
> is a single element, nothing more.

Consider 123 and "123".  How many elements is/are each of them?

Remember, you have repeatedly stated your desire for them to be
treated the same way -- you DO remember that, right?  Drawing any
distinction between them needs that type concept which you so
deplore, you want to have "just data".  So?

list("123") return ["1", "2", "3"].  A string IS a sequence (whose single
elements are 1-character-strings), so list() happily does its thing.  If
I want a list with a single element in it, i use ["123"], of course, *NOT*
list("123")!

What do you think list(123) should return?  Or list(-1)?  Are these
single elements, or are they equivalent to "123" (three elements)
and "-1" (two elements) respectively?  What about list(+1)...?-)

And why ever should, as you've asked for elsewhere, list(None)
return an empty list rather than the list with the single element
None?  I can't see any rhyme or reason to the sum total of your
expressed desiderata.


>     Ah, but getting back to that point I said to remember.
>
> foo = "a"
> foo = list(foo)
>
>     foo is not ['a'].  It took a single element and made it a list.  So we
now

No, it took a one-element sequence (a string) and made it a one-element
list.  That IS exactly list()'s job.  A string IS a sequence.  ANY string IS
a
sequence.  Just like any list is a sequence, any tuple is a sequence, any
object is a sequence if it defines __getitem__ (and possibly __length__).

> know that a single element has a sequence.  Here is another sequence of a
> single element.
>
> foo = 1
> foo = list(foo)
>
>     Error.  Single element sequence.  Remember, any sequence can either be
a
> single value or a sequence.  Yet here we have a single value denied.
Quirks
> abound.

I can see no quirk whatsoever in this behaviour.  Maybe what is confusing
you is exactly the simplification which both you and I approved recently: a
string is a sequence *whose elements are themselves strings* (strings of
one element each).  This is a mathematical anomaly -- Frege and others
tried to found mathematics on such a basis (sets whose elements are sets,
etc) and it just crumbled all around them.  But, fortunately, we can afford
to
be pragmatical here, relying on the homomorphisms between the set of
characters and the set of one-character strings -- they ARE finite sets,
after
all, so no real paradox can be hidden therein:-) -- as long as we DO know
it's just a pragmatical "mental short-hand" to avoid the need for yet
another
separate type, that of characters, for which there is really no practical
call.
But don't let that confuse into claiming that 'a' is "a single element" --
it's
really a 1-element sequence, just as ['a'], ('a'), etc.


Alex






More information about the Python-list mailing list