[Tutor] when string,join won't work
Tim Peters
tim.one@home.com
Thu, 4 Jan 2001 23:11:21 -0500
[Moshe Zadka]
> ...
> But it doesn't: ``str'' works on any type (almost). Python is
> strongly typed: you can't treat a number as a string, or vice
> versa. I can't begin to explain how many times it caused
> potentially yucky bugs into obvious ones. It is, however,
> polymorphic to extremes, which is why map(str, seq)
> works always: you don't need to promise what types are in seq.
>
> Explicit is Better then Implicit is one of the Python rules of
> living. Python will stay explicit, so whether you think it's
> silly or not, this is an essential feature, not a quirk.
In the specific case of string.join, though, it is indeed pointless to make
the user explicitly convert everything to a string themself. While errors
about mixing strings with (for example) integers in arithmetic catches
*many* genuine bugs in code that (for example) Perl lets pass silently, I've
never seen string.join griping about a non-string sequence element catch a
*logic* error. Of *course* the user wants everything converted to a string,
else they wouldn't have called string.join to begin with! The failure to
convert all the sequence elements to strings first by hand is simply a
failure to comply with an anal implementation restriction. IOW, in every
case I've seen where string.join raised this error, the fix was *invariably*
to do the map(str, seq) business by hand first: the problem was never
deeper than that. The same isn't true of, e.g., griping about
"4" + 2
(6 or "42" intended?). But there's no ambiguity about what the user intends
when they call string.join(["4", 2]): they've already been explicit about
their intent.
if-i-ask-for-a-string-i-shouldn't-be-surprised-if-i-get-a-string-ly
y'rs - tim