Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #11696
| Date | 2011-08-17 17:55 +0100 |
|---|---|
| From | MRAB <python@mrabarnett.plus.com> |
| Subject | Re: Syntactic sugar for assignment statements: one value to multiple targets? |
| References | <16ea4848-db0c-489a-968c-ca40700f5806@m5g2000prh.googlegroups.com> <j2eki3$43t$1@dont-email.me> <7f30e39b-4e4f-4426-b819-b4670871d199@df3g2000vbb.googlegroups.com> <mailman.117.1313565232.27778.python-list@python.org> <c6cd02ea-3020-4970-82c6-8d289738bf37@o11g2000yql.googlegroups.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.135.1313600157.27778.python-list@python.org> (permalink) |
On 17/08/2011 10:26, gc wrote:
> On Aug 17, 3:13 am, Chris Angelico<ros...@gmail.com> wrote:
>
>> Minor clarification: You don't want to initialize them to the same
>> value, which you can do already:
>>
>> a=b=c=d=e=dict()
>
> Right. Call the proposed syntax the "instantiate separately for each
> target" operator. (It can be precisely defined as a * on the RHS of a
> one-into-many assignment statement--i.e. an assignment statement with
> 1 object on the RHS and more than 1 on the LHS).
>
I think that lazy unpacking is the more important issue because we can
replace instantiation with copying:
def copies(obj, count=None):
if count is None:
while True:
yield obj.copy()
else:
for i in range(count):
yield obj.copy()
(Should it yield deep copies, or should there be a separate deep_copies
function?)
> It has only one very modest function, which is to unpack
>
> a, b, c, d, e = *dict()
>
> to
>
> a, b, c, d, e = dict(), dict(), dict(), dict(), dict()
>
This becomes:
a, b, c, d, e = copies(dict(), 5)
With lazy unpacking it would become:
a, b, c, d, e = lazy copies(dict())
(Or whatever the syntax is.)
> so that you have n separate objects instead of one. If you want the
> same object duplicated five times, you'd best use a=b=c=d=e=dict().
> (I'd guess that 90% of the people who try the a=b=c version actually
> *want* separate objects and are surprised at what they get--I made
> that mistake a few times!--but changing either behavior would be a
> very bad idea. This proposed syntax would be the Right Way to get
> separate objects.)
>
> Maybe this is more visibly convenient with a complex class, like
>
> x, y, z = *SuperComplexClass(param1, param2, kwparam = "3", ...)
>
x, y, z = lazy copies(SuperComplexClass(param1, etc, ...))
[snip]
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Syntactic sugar for assignment statements: one value to multiple targets? gc <gc1223@gmail.com> - 2011-08-02 18:45 -0700
Re: Syntactic sugar for assignment statements: one value to multiple targets? Chris Angelico <rosuav@gmail.com> - 2011-08-03 03:09 +0100
Re: Syntactic sugar for assignment statements: one value to multiple targets? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-08-03 18:25 +1000
Re: Syntactic sugar for assignment statements: one value to multiple targets? Tim Chase <python.list@tim.thechases.com> - 2011-08-03 06:15 -0500
Re: Syntactic sugar for assignment statements: one value to multiple targets? Tim Chase <python.list@tim.thechases.com> - 2011-08-03 06:25 -0500
Re: Syntactic sugar for assignment statements: one value to multiple targets? gc <gc1223@gmail.com> - 2011-08-16 12:50 -0700
Re: Syntactic sugar for assignment statements: one value to multiple targets? Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2011-08-03 20:29 +1200
Re: Syntactic sugar for assignment statements: one value to multiple targets? "Martin P. Hellwig" <martin.hellwig@gmail.com> - 2011-08-16 21:39 +0100
Re: Syntactic sugar for assignment statements: one value to multiple targets? gc <gc1223@gmail.com> - 2011-08-16 17:14 -0700
Re: Syntactic sugar for assignment statements: one value to multiple targets? MRAB <python@mrabarnett.plus.com> - 2011-08-17 03:11 +0100
Re: Syntactic sugar for assignment statements: one value to multiple targets? Chris Angelico <rosuav@gmail.com> - 2011-08-17 08:13 +0100
Re: Syntactic sugar for assignment statements: one value to multiple targets? gc <gc1223@gmail.com> - 2011-08-17 02:26 -0700
Re: Syntactic sugar for assignment statements: one value to multiple targets? Chris Angelico <rosuav@gmail.com> - 2011-08-17 10:45 +0100
Re: Syntactic sugar for assignment statements: one value to multiple targets? gc <gc1223@gmail.com> - 2011-08-17 03:33 -0700
Re: Syntactic sugar for assignment statements: one value to multiple targets? Terry Reedy <tjreedy@udel.edu> - 2011-08-17 12:12 -0400
Re: Syntactic sugar for assignment statements: one value to multiple targets? MRAB <python@mrabarnett.plus.com> - 2011-08-17 17:55 +0100
Re: Syntactic sugar for assignment statements: one value to multiple targets? Chris Angelico <rosuav@gmail.com> - 2011-08-17 18:25 +0100
Re: Syntactic sugar for assignment statements: one value to multiple targets? "OKB (not okblacke)" <brenNOSPAMbarn@NObrenSPAMbarn.net> - 2011-08-17 19:13 +0000
Re: Syntactic sugar for assignment statements: one value to multiple targets? Zero Piraeus <schesis@gmail.com> - 2011-08-17 16:07 -0400
Re: Syntactic sugar for assignment statements: one value to multiple targets? Ethan Furman <ethan@stoneleaf.us> - 2011-08-17 12:52 -0700
Re: Syntactic sugar for assignment statements: one value to multiple targets? John Pinner <funthyme@gmail.com> - 2011-08-18 02:26 -0700
Re: Syntactic sugar for assignment statements: one value to multiple targets? Roy Smith <roy@panix.com> - 2011-08-18 09:13 -0400
csiph-web