Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old December 14th, 2005, 06:05 PM
Carl J. Van Arsdall
Guest
 
Posts: n/a
Default Question about tuple lengths


From my interpreter prompt:
[color=blue][color=green][color=darkred]
>>> tuple = ("blah")
>>> len(tuple)[/color][/color][/color]
4[color=blue][color=green][color=darkred]
>>> tuple2 = ("blah",)
>>> len (tuple2)[/color][/color][/color]
1

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?

--

Carl J. Van Arsdall
cvanarsdall@mvista.com
Build and Release
MontaVista Software

  #2  
Old December 14th, 2005, 08:05 PM
MM Zeeman
Guest
 
Posts: n/a
Default Re: Question about tuple lengths

Carl J. Van Arsdall wrote:
[color=blue]
>
> From my interpreter prompt:
>[color=green][color=darkred]
> >>> tuple = ("blah")
> >>> len(tuple)[/color][/color]
> 4[color=green][color=darkred]
> >>> tuple2 = ("blah",)
> >>> len (tuple2)[/color][/color]
> 1
>
> 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?
>[/color]

Hello,

Thats because the expression ("blah") actually resolves to "blah" instead of
a tuple containing the string "blah".
[color=blue][color=green][color=darkred]
>>> type(("spam"))[/color][/color][/color]
<type 'str'>

Adding a comma after spam results in the tuple being created.
[color=blue][color=green][color=darkred]
>>> type(("spam",))[/color][/color][/color]
<type 'tuple'>

And to make things even more confusing, just adding a comma without braces
will give you a tuple too.
[color=blue][color=green][color=darkred]
>>> "spam",[/color][/color][/color]
('spam',)

The explanation for this all can be found at:
http://docs.python.org/ref/parenthesized.html

Regards,

Maas


  #3  
Old December 14th, 2005, 10:05 PM
Steven D'Aprano
Guest
 
Posts: n/a
Default Re: Question about tuple lengths

On Wed, 14 Dec 2005 09:54:31 -0800, Carl J. Van Arsdall wrote:
[color=blue]
>
> From my interpreter prompt:
>[color=green][color=darkred]
> >>> tuple = ("blah")[/color][/color][/color]

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

[color=blue][color=green][color=darkred]
> >>> len(tuple)[/color][/color]
> 4[/color]

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 ().

[color=blue][color=green][color=darkred]
> >>> tuple2 = ("blah",)
> >>> len (tuple2)[/color][/color]
> 1[/color]

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',)

[color=blue]
> 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?[/color]

It's not a bug.


--
Steven.

  #4  
Old December 15th, 2005, 06:45 PM
Larry Bates
Guest
 
Posts: n/a
Default Re: Question about tuple lengths

Carl J. Van Arsdall wrote:[color=blue]
>
> From my interpreter prompt:
>[color=green][color=darkred]
>>>> tuple = ("blah")
>>>> len(tuple)[/color][/color]
> 4[color=green][color=darkred]
>>>> tuple2 = ("blah",)
>>>> len (tuple2)[/color][/color]
> 1
>
> 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?[/color]

Additional note:

You should not give a tuple a variable name of tuple, it masks
the built-in tuple() function. This also goes for str, or other
built-ins. This will "bite" you at some point if it hasn't already.

-Larry Bates
 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles