Re: Parens do create a tuple (was: one-element tuples [Was: Most probably a stupid question, but I still want to ask])

2016-04-10 Thread Tim Chase
On 2016-04-11 10:45, Ben Finney wrote:
> Also, there is another obvious way to create an empty tuple: call
> the ‘tuple’ type directly:
> 
> >>> foo = tuple()
> >>> print(type(foo), len(foo))  
>  0

But here the parens make the tuple too:

  >>> foo = tuple
  >>> print(type(foo))
  
  >>> len(foo)
  Traceback (most recent call last):
File "", line 1, in 
  TypeError: object of type 'type' has no len()

(totally just yanking chains, throwing pebbles in the pond to watch
the ripples, and otherwise sewing confusion ;-)

-tkc




-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Parens do create a tuple (was: one-element tuples [Was: Most probably a stupid question, but I still want to ask])

2016-04-10 Thread Stephen Hansen
On Sun, Apr 10, 2016, at 05:45 PM, Ben Finney wrote:
> So, let's please stop saying “parens don't create a tuple”. They do, and
> because of that I've stopped saying that false over-simplification.

I stand by "parens don't make a tuple", with the caveat that I should
have mentioned the empty tuple exception that proves the rule. The only
time you need parens is to resolve ambiguity.

To suggest that parens do make tuples confuses the issue, given things
like this:
>>> a = 1,2,3
>>> b = (1, 2, 3)

--
Stephen Hansen
  m e @ i x o k a i  . i o
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Parens do create a tuple (was: one-element tuples [Was: Most probably a stupid question, but I still want to ask])

2016-04-10 Thread Chris Angelico
On Mon, Apr 11, 2016 at 10:45 AM, Ben Finney  wrote:
> So the expanation that remains true when you examine it is: People
> wanted a literal syntax to create a zero-length tuple. A pair of parens
> is that literal syntax, and it's the parens that create the (empty)
> tuple.

But parens do NOT create a one-element tuple, and that's usually where
people trip up. If you show someone this line of code:

x = ()

and ask what x will be, you might get some wrong responses, but you'll
get a lot of people correctly deducing that it's a tuple. The problem
is that people see this progression:

x = ()
y = (1)
z = (1, 2)

and assume they're all tuples. A better progression is this:

x = ()
y = (1,)
z = (1, 2,)

where every element is followed by a comma and every tuple is
surrounded by parentheses. In that situation, everything works. There
are slightly different rules about which parts are optional (the
parens everywhere except the first case, and the last comma everywhere
except the second), but this should be the basic form of tuple
progression.

ChrisA
-- 
https://mail.python.org/mailman/listinfo/python-list


Parens do create a tuple (was: one-element tuples [Was: Most probably a stupid question, but I still want to ask])

2016-04-10 Thread Ben Finney
Stephen Hansen  writes:

> […] parens don't make tuples, commas do.


Chris Angelico  writes:

> The thing you're confused at is that it's not the parentheses that
> create a tuple. Parentheses merely group.


MRAB  writes:

> As has been said already, it's the comma that makes the tuple. The
> parentheses are often needed to avoid ambiguity.

This is too simplistic, and IMO it's just sowing the seed for future
confusion.

As MRAB states in the same message:

> There _is_ one exception though: (). It's the empty tuple (a 0-element
> tuple). It doesn't have a comma and the parentheses are mandatory.
> There's no other way to write it.

So, let's please stop saying “parens don't create a tuple”. They do, and
because of that I've stopped saying that false over-simplification.

A pair of tuples as an expression is literal syntax to create a tuple
with zero items.

Also, there is another obvious way to create an empty tuple: call the
‘tuple’ type directly:

>>> foo = tuple()
>>> print(type(foo), len(foo))
 0

So the expanation that remains true when you examine it is: People
wanted a literal syntax to create a zero-length tuple. A pair of parens
is that literal syntax, and it's the parens that create the (empty)
tuple.

-- 
 \ “It is hard to believe that a man is telling the truth when you |
  `\  know that you would lie if you were in his place.” —Henry L. |
_o__)  Mencken |
Ben Finney

-- 
https://mail.python.org/mailman/listinfo/python-list