Question about tuple lengths

Steven D'Aprano steve at REMOVETHIScyber.com.au
Wed Dec 14 16:49:30 EST 2005


On Wed, 14 Dec 2005 09:54:31 -0800, Carl J. Van Arsdall wrote:

> 
>  From my interpreter prompt:
> 
>  >>> tuple = ("blah")

There is a special place in Hell reserved for people who overwrite
built-in functions like tuple(), list(), str() and so forth. *wink*


>  >>> len(tuple)
> 4

Brackets on their own are just used for grouping, so ("blah") is the same
as just "blah", namely a string with four characters.

The only exception to this is the empty tuple, which is made with an empty ().


>  >>> tuple2 = ("blah",)
>  >>> len (tuple2)
> 1

Tuples are made with commas, not brackets. You could write just "blah",
without the brackets and it would still work.

py> t = "blah",
py> t
('blah',)

 
> So why is a tuple containing the string "blah" without the comma of 
> length four? Is there a good reason for this or is this a bug?

It's not a bug.


-- 
Steven.




More information about the Python-list mailing list