dictionary/hash and '1' versus 1

Reedick, Andrew jr9445 at ATT.COM
Mon Jan 7 18:09:50 CET 2008



> -----Original Message-----
> From: python-list-bounces+jr9445=att.com at python.org [mailto:python-
> list-bounces+jr9445=att.com at python.org] On Behalf Of Steven D'Aprano
> Sent: Saturday, January 05, 2008 7:01 PM
> To: python-list at python.org
> Subject: Re: dictionary/hash and '1' versus 1
> 
> 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'


Bingo.  Perl has specific operators to establish intent:
	> Perl -e "'1' + 1"
	> 2
	> Perl -e "'1' . 1"
	> 11
'+' is the operator for addition
'.' is the operator for string concatenation

int and string comparisons also have specific operators:
	$a == $b  # compare as integers:  ==,  >,  <, <=, >=
	$a eq $b  # compare as strings:   eq, gt, lt, le, ge


Which now morphs the conversation into the issue of how too much
operator overloading creates confusion and/or ambiguity.



*****

The information transmitted is intended only for the person or entity to which it is addressed and may contain confidential, proprietary, and/or privileged material. Any review, retransmission, dissemination or other use of, or taking of any action in reliance upon this information by persons or entities other than the intended recipient is prohibited. If you received this in error, please contact the sender and delete the material from all computers. GA623





More information about the Python-list mailing list