Why am I getting a strange 0 2 result when I give this to Python?
#tuples ~wtF?
a=()
b=(a)
c=(a,1)
len(b)
len(c)
NB: I get an expected 1 2 result for lists:
a=[]
b=[a]
c=[a,1]
len(b)
len(c)
This is happening on Linux:
$ python --version
Python 2.7.2+
Edit: wrt answers so far
So is this somehow because of the , in the c=(a,1) assignment?
>>> print b
()
>>> print c
((), 1)
The brackets don’t make it a tuple – the comma does. Consider:
The brackets there mean ‘do this first’. The brackets in:
Mean the same. So, this is equivalent to
so
b is awill beTrue.To make
ba tuple containing the empty tuple, you need to do:Again, the brackets don’t make it a tuple (except for the special case of
()is the empty tuple), the comma does.For the edit,
Since
a = (), this is the same as:ie, it is a tuple containing the empty tuple and
1.()is always the empty tuple (same as[]is the empty list), but this it the only time the brackets mean ‘tuple’. The above is the same as:Though normally people do include the brackets here (and the
reprandstrof tuples always do), this is for style rather than because they’re meaningful.