assert type([]) == type(())

Mel mwilson at the-wire.com
Sat Jan 2 14:00:18 EST 2010


VanceE wrote:

> Just curious.
> 
> type([]) == type(())
> is False as expected
> 
> and
> 
> assert type([]) == type(())
> throws an AssertionError as expected.
> 
> However the following is not an error
> 
> for x in []:
>     assert type(x) == type(())
> 
> I expected an AssertionError but get no errors at all.
> Any explaination?

Others have said what happens.  Bit of a mind bender -- here's the smallest 
change to do what you expected:

Python 2.6.2 (release26-maint, Apr 19 2009, 01:56:41) 
[GCC 4.3.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> for x in [],:
...   assert type(x) == type (())
... 
Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
AssertionError
>>> 


Because of the comma, this creates a tuple containing the empty list, and 
iterates over the tuple.

	Mel.




More information about the Python-list mailing list