Make Python create a tuple with one element in a clean way
Asun Friere
afriere at yahoo.co.uk
Sun May 11 21:37:23 EDT 2008
On May 11, 10:54 pm, wxPytho... at gmail.com wrote:
> To create a tuple with one element, you need to do this:
>
> >>> my_tuple = (1,) # Note the trailing comma after the value 1
> >>> type(my_tuple)
>
> <type 'tuple'>
>
You needn't at all. You could simply do this:
>>> your_tuple = 1,
You see, it's not the parentheses that make the tuple.
> But if you do this
>
> >>> my_tuple = (1)
> >>> type(my_tuple)
>
> <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. ;)
More information about the Python-list
mailing list