[Python-3000] Python 3000 Status Update (Long!)
Steve Howell
showell30 at yahoo.com
Thu Jun 21 20:04:37 CEST 2007
--- Bill Janssen <janssen at parc.com> wrote:
> > My other concern with sum() is just the common
> pitfall
> > that you do sum(line_of_numbers.split(',')) and
> get
> > '35' when you intended to write code to get 8.
> I'd
> > rather have that fail obviously than subtlely.
>
> Common pitfall? I doubt it. Possible pitfall?
> Sure.
>
It's a common mistake, for me anyway, to forgot to
cast something that I just read from a file into an
integer before performing arithmetic on it. But it's
usually not a pitfall now. It's just a quick
exception that I can quickly diagnose and fix.
Try this code under Python 2:
name, amount, tip = 'Bill,20,1.5'.split(',')
print name + ' payed ' + sum(amount,tip)
It will throw an obvious exception.
Obviously, this is a pitfall even under current
Python:
name, amount, tip = 'Bill,20,1.5'.split(',')
print name + ' payed ' + amount + tip
So then you have three choices on how to improve
Python, one of which you sort of alluded to in your
other reply:
1) Eliminate the current pitfall by introducing
another operator for concatenation.
2) Keep sum() as it is, but make the error message
more clear when somebody uses it on strings. Example:
Sum() cannot be used to join strings. Perhaps you
meant to use ''.join().
3) Make sum() have a consistent pitfall with the
"+" operator, even though English/Python has a lot
more latitude with words than punctuation when it
comes to disambiguating concepts.
IMHO #1 is too extreme, #2 is the best option, and #3
doesn't really solve any practical problems. The
arguments for #3 seems to come from consistency/purity
vantages, which are fine, but not as important as
usability.
I concede this entire argument is based on the perhaps
shaky premise that *most* people never forget to turn
strings into integers, but I fully admit my
fallibility in this regard.
____________________________________________________________________________________
Get the Yahoo! toolbar and be alerted to new email wherever you're surfing.
http://new.toolbar.yahoo.com/toolbar/features/mail/index.php
More information about the Python-3000
mailing list