Make Python create a tuple with one element in a clean way

W

wxPythoner

To create a tuple with one element, you need to do this:
<type 'tuple'>


But if you do this
<type 'int'>

you don't get a tuple. I thought that just putting a value inside ( )
would make a tuple. Apparently that is not the case. I hate ugly code
so it would be clean if Python would convert anything put into ( ) to
be a tuple, even if just one value was put in (without having to use
that ugly looking comma with no value after it).
 
F

Filip Štědronský

Hello,
so it would be clean if Python would convert anything put into ( ) to
be a tuple, even if just one value was put in (without having to use
that ugly looking comma with no value after it).

if it worked that way, it will absolutely mess up Python syntax, because
we mathematicians are used to use parentheses to force explicit operator
precedence, to group expressions, etc.

Should (1+1)*2 yield 4 or (2,2) ?
 
A

alex23

I thought that just putting a value inside ( )
would make a tuple. Apparently that is not the case.

It's not the case at all. Check out the Tuples & Sequences section in
the python docs at http://docs.python.org/tut/node7:

"A tuple consists of a number of values separated by commas"

So it's not the parentheses that define it as a tuple, but the comma.
<type 'tuple'>

Hope this helps.

- alex23
 
A

Asun Friere

To create a tuple with one element, you need to do this:


<type 'tuple'>

You needn't at all. You could simply do this:

You see, it's not the parentheses that make the tuple.

But if you do this


<type 'int'>

you don't get a tuple.

For which the BDFL should make us eternally grateful.
it would be clean if Python would convert anything put into ( ) to
be a tuple

You seriously want 2*(3+4) to return (7,7)? You call that "clean"?!

At least type(my_tuple) would always return 'tuple,' whether it was or
not. ;)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top