473,386 Members | 1,720 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Question about tuple lengths


From my interpreter prompt:
tuple = ("blah")
len(tuple) 4 tuple2 = ("blah",)
len (tuple2)

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
cv*********@mvista.com
Build and Release
MontaVista Software

Dec 14 '05 #1
3 1475
Carl J. Van Arsdall wrote:

From my interpreter prompt:
>>> tuple = ("blah")
>>> len(tuple) 4 >>> tuple2 = ("blah",)
>>> len (tuple2) 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?


Hello,

Thats because the expression ("blah") actually resolves to "blah" instead of
a tuple containing the string "blah".
type(("spam")) <type 'str'>

Adding a comma after spam results in the tuple being created.
type(("spam",)) <type 'tuple'>

And to make things even more confusing, just adding a comma without braces
will give you a tuple too.
"spam",

('spam',)

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

Regards,

Maas
Dec 14 '05 #2
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.

Dec 14 '05 #3
Carl J. Van Arsdall wrote:

From my interpreter prompt:
tuple = ("blah")
len(tuple) 4 tuple2 = ("blah",)
len (tuple2)

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?


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
Dec 15 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: T. Kaufmann | last post by:
Hi there, A simple but important question: How can I initialize a super class (like dict) correctly in my subclass constructor? A sample: class MyClass(dict): def __init__(self):
50
by: Will McGugan | last post by:
Hi, Why is that a tuple doesnt have the methods 'count' and 'index'? It seems they could be present on a immutable object. I realise its easy enough to convert the tuple to a list and do this,...
32
by: David | last post by:
Hi I'm trying to teach myself python and so far to good, but I'm having a bit of trouble getting a function to work the way I think it should work. Right now I'm taking a simple program I wrote in...
6
by: PengYu.UT | last post by:
Hi, Suppose I have the following class declaration with a pointer as the argument. And suppose I "new" an array "lengths" as the argument "edge_lengths". Now, the polygon object is valid....
2
by: Bastian Voigt | last post by:
Hallo List! I just found this old posting on google. Now my question is how can I read an integer value from the PGresult using the binary format? Can someone plz gimme a code example? (please...
105
by: Christoph Zwerschke | last post by:
Sometimes I find myself stumbling over Python issues which have to do with what I perceive as a lack of orthogonality. For instance, I just wanted to use the index() method on a tuple which does...
5
by: abcd | last post by:
I wanted to extend tuple but ran into a problem. Here is what I thought would work class MyTuple(tuple): def __init__(self, *args): tuple.__init__(self, args) x = MyTuple(1,2,3,4) That...
4
by: Johannes Bauer | last post by:
Hello group, I'm currently doing something like this: import time localtime = time.localtime(1234567890) fmttime = "%04d-%02d-%02d %02d:%02d:%02d" % (localtime, localtime, localtime,...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.