Re: A new to Python question
- From: bokr@xxxxxx (Bengt Richter)
- Date: Sun, 15 May 2005 08:45:20 GMT
On Sun, 15 May 2005 08:00:47 +0200, "Fredrik Lundh" <fredrik@xxxxxxxxxxxxxx> wrote:
>M.E.Farmer wrote:
>
>> I said exactly what I meant, the parentheses around the values creates
>> a tuple that you have no reference to!
>
>repeating it doesn't make you right; no extra tuple is created, and the
>parens are part of the syntax:
>
> If the target is a target list enclosed in parentheses or in square
> brackets: The object must be a sequence with the same number
> of items as there are targets in the target list, and its items are
> assigned, from left to right, to the corresponding targets.
>
> http://docs.python.org/ref/assignment.html
>
>(originally, you had to use [] to unpack lists, and () or no parens only
>worked for tuples. the ability to use an arbitrary sequence was added
>in 1.5)
>
Note that (x) without a comma doesn't unpack like [x] however:
>>> (x)= 123,
>>> x
(123,)
>>> [x]= 123,
>>> x
123
>>> (x,)= 123,
>>> x
123
>>> x,= 123,
>>> x
123
Also, BTW,
>>> 123,
(123,)
>>> [123,]
[123]
I.e., that last result is not [(123,)]
There's actually a bunch of context-sensitive things about commas that
you just have to get used to, in lists, tuples, function call arg lists,
subscripts (__getitem__ args), unpacking assignment targets, etc.
>on the other hand, the function you're calling in this example *does*
>create a tuple that you have no reference to after the assignment.
Yeah, but it would create that tuple whether there was an assignment
of the returned result or not. Collecting unconnected facts in one
breath remind me too much of political speeches ;-)
>that doesn't matter, of course, since the tuple is removed by the
>garbage collector immediately after it has been unpacked.
I'm too tired to figure a humorous segue ;-)
Regards,
Bengt Richter
.
- Follow-Ups:
- Re: A new to Python question
- From: M.E.Farmer
- Re: A new to Python question
- References:
- A new to Python question
- From: David
- Re: A new to Python question
- From: Bernd Nawothnig
- Re: A new to Python question
- From: M.E.Farmer
- Re: A new to Python question
- From: Steven Bethard
- Re: A new to Python question
- From: M.E.Farmer
- Re: A new to Python question
- From: Fredrik Lundh
- A new to Python question
- Prev by Date: pydoc for global variables
- Next by Date: Re: Modifying a built-in function for logging purposes
- Previous by thread: Re: A new to Python question
- Next by thread: Re: A new to Python question
- Index(es):
Relevant Pages
|