[1,2,3] exactly same as [1,2,3,] ?

Steven D'Aprano steve at REMOVE-THIS-cybersource.com.au
Fri Aug 29 16:59:48 CEST 2008


On Thu, 28 Aug 2008 16:28:03 -0700, Paul McNett wrote:

> mh at pixar.com wrote:
>> x=[1,2,3]
>> and
>> x=[1,2,3,]
>> 
>> are exactly the same, right?
> 
> When confronted with this type of question, I ask the interpreter:
> 
> {{{
> mac:~ pmcnett$ python
> Python 2.5.2 (r252:60911, Feb 22 2008, 07:57:53) [GCC 4.0.1 (Apple
> Computer, Inc. build 5363)] on darwin Type "help", "copyright",
> "credits" or "license" for more information.
>  >>> [1,2,3] == [1,2,3,]
> True
> }}}



Putting on my pedantic hat...

In this case you're right about the two lists being the same, and I'm a 
great believer in checking things in the interactive interpreter, but you 
need to take care. Just because two objects compare as equal doesn't 
*necessarily* mean they are the same:


>>> 1.0 == 1
True
>>> 1.0 == decimal.Decimal('1.0')
False
>>> 1.0 == float(decimal.Decimal('1.0'))
True
>>> collections.defaultdict(999) == {}
True



-- 
Steven



More information about the Python-list mailing list