dictionary/hash and '1' versus 1
Steven D'Aprano
steve at REMOVE-THIS-cybersource.com.au
Sat Jan 5 19:00:53 EST 2008
On Sat, 05 Jan 2008 15:07:10 -0800, bearophileHUGS wrote:
> Paddy:
>> Not really, it seems to me to be going the exact opposite way with
>> languages with automatic type conversions being seen as not suited for
>> larger programs.
>
> In Java you can add the number 1 to a string, and have it automatically
> converted to string before the string join... What do you think of that
> feature?
You mean special-casing the int 1 so that this works?
# Faked!
>>> x = "spam spam spam"
>>> x = x + 1
>>> x
'spam spam spam1'
but not this?
>>> x = "spam spam spam"
>>> x = x + 2
TypeError: automatic conversion between strings and ints only works for
the int 1
How bizarre.
The problem with automatic conversions between strings and integers is
that it isn't clear what the user wants when they do something like this:
>>> x = '1' + 1
Should x be the string '11' or the int 2? Please justify your answer.
On the other hand, if the language includes separate operators for
addition and concatenation (say, + and &) then that sort of auto-
conversion is no longer ambiguous:
>>> '2' + 3
5
>>> '2' & 3
'23'
--
Steven
More information about the Python-list
mailing list