Re: Syntactic sugar for assignment statements: one value to multiple targets?

2011-08-18 Thread John Pinner
On Aug 3, 2:45 am, gc gc1...@gmail.com wrote: Hi everyone! Longtime lurker, hardly an expert, but I've been using Python for various projects since 2007 and love it. I'm looking for either (A) suggestions on how to do a very common operation elegantly and Pythonically, or (B) input on whether

Re: Syntactic sugar for assignment statements: one value to multiple targets?

2011-08-18 Thread Roy Smith
In article 16ea4848-db0c-489a-968c-ca40700f5...@m5g2000prh.googlegroups.com, gc gc1...@gmail.com wrote: I frequently need to initialize several variables to the same value, as I'm sure many do. Sometimes the value is a constant, often zero; sometimes it's more particular, such as

Re: Syntactic sugar for assignment statements: one value to multiple targets?

2011-08-17 Thread Chris Angelico
On Wed, Aug 17, 2011 at 1:14 AM, gc gc1...@gmail.com wrote: Perfectly reasonable request! Maybe there aren't as many cases when multiple variables need to be initialized to the same value as I think there are. Minor clarification: You don't want to initialize them to the same value, which you

Re: Syntactic sugar for assignment statements: one value to multiple targets?

2011-08-17 Thread gc
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

Re: Syntactic sugar for assignment statements: one value to multiple targets?

2011-08-17 Thread Chris Angelico
On Wed, Aug 17, 2011 at 10:26 AM, gc gc1...@gmail.com 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

Re: Syntactic sugar for assignment statements: one value to multiple targets?

2011-08-17 Thread gc
On Aug 17, 5:45 am, Chris Angelico ros...@gmail.com wrote: (snip) Right. Call the proposed syntax the instantiate separately for each target operator. (snip) It might just as easily be some other function call; for instance: head1,head2,head3=file.readline() Hm--that's interesting! OK,

Re: Syntactic sugar for assignment statements: one value to multiple targets?

2011-08-17 Thread Terry Reedy
The issue behind this thread is that for immutable objects, binding to n copies has the same effect as n bindings to one object (so one does not really have to know which one is doing), whereas the two are different for mutable objects (so one does have to know). In short, identity matters for

Re: Syntactic sugar for assignment statements: one value to multiple targets?

2011-08-17 Thread MRAB
On 17/08/2011 10:26, gc wrote: On Aug 17, 3:13 am, Chris Angelicoros...@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.

Re: Syntactic sugar for assignment statements: one value to multiple targets?

2011-08-17 Thread Chris Angelico
On Wed, Aug 17, 2011 at 5:55 PM, MRAB pyt...@mrabarnett.plus.com wrote: x, y, z = lazy copies(SuperComplexClass(param1, etc, ...)) This assumes that you can construct it once and then copy it reliably, which may mean that the class implement copying correctly. It also wouldn't work with: a, b,

Re: Syntactic sugar for assignment statements: one value to multiple targets?

2011-08-17 Thread OKB (not okblacke)
gc wrote: Maybe this is more visibly convenient with a complex class, like x, y, z = *SuperComplexClass(param1, param2, kwparam = 3, ...) where you need three separate objects but don't want to duplicate the class call (for obvious copy-paste reasons) and where bundling it in a list

Re: Syntactic sugar for assignment statements: one value to multiple targets?

2011-08-17 Thread Ethan Furman
gc wrote: Target lists using comma separation are great, but they don't work very well for this task. What I want is something like a,b,c,d,e = *dict() This isn't going to happen. From all the discussion so far I think your best solution is a simple helper function (not tested): def

Re: Syntactic sugar for assignment statements: one value to multiple targets?

2011-08-17 Thread Zero Piraeus
: Off on a tangent ... On 16 August 2011 20:14, gc gc1...@gmail.com wrote: Let me address one smell from my particular example, which may be the one you're noticing. If I needed fifty parallel collections I would not use separate variables; I've coded a ghastly defaultdefaultdict just for

Re: Syntactic sugar for assignment statements: one value to multiple targets?

2011-08-16 Thread gc
Thanks for all the discussion on this. Very illuminating. Sorry for the long delay in responding--deadlines intervened. I will use the list comprehension syntax for the foreseeable future. Tim, I agree with you about the slurping in final position--it's actually quite surprising. As I'm sure you

Re: Syntactic sugar for assignment statements: one value to multiple targets?

2011-08-16 Thread Martin P. Hellwig
On 03/08/2011 02:45, gc wrote: cut a,b,c,d,e = *dict() where * in this context means something like assign separately to all. CUT Any thoughts? Thanks! Well got a thought but I am afraid it is the opposite of helpful in the direct sense. So if you don't want to hear it skip it :-)

Re: Syntactic sugar for assignment statements: one value to multiple targets?

2011-08-16 Thread gc
On Aug 16, 4:39 pm, Martin P. Hellwig martin.hell...@gmail.com wrote: On 03/08/2011 02:45, gc wrote: cut a,b,c,d,e = *dict() where * in this context means something like assign separately to all. snip . . . it has a certain code smell to it. snip I would love to see an example where

Re: Syntactic sugar for assignment statements: one value to multiple targets?

2011-08-16 Thread MRAB
On 17/08/2011 01:14, gc wrote: On Aug 16, 4:39 pm, Martin P. Hellwigmartin.hell...@gmail.com wrote: On 03/08/2011 02:45, gc wrote: cut a,b,c,d,e = *dict() where * in this context means something like assign separately to all. snip . . . it has a certain code smell to it.snip I would

Re: Syntactic sugar for assignment statements: one value to multiple targets?

2011-08-03 Thread Steven D'Aprano
gc wrote: Target lists using comma separation are great, but they don't work very well for this task. What I want is something like a,b,c,d,e = *dict() a, b, c, d, e = [dict() for i in range(5)] Unfortunately there is no way of doing so without counting the assignment targets. While

Re: Syntactic sugar for assignment statements: one value to multiple targets?

2011-08-03 Thread Gregory Ewing
gc wrote: Alternatively, is there a version of iterable multiplication that creates new objects rather than just copying the reference? You can use a list comprehension: a, b, c, d, e = [dict() for i in xrange(5)] or a generator expression: a, b, c, d, e = (dict() for i in xrange(5))

Re: Syntactic sugar for assignment statements: one value to multiple targets?

2011-08-03 Thread Katriel Cohn-Gordon
On Wed, Aug 3, 2011 at 9:25 AM, Steven D'Aprano steve+comp.lang.pyt...@pearwood.info wrote: gc wrote: Target lists using comma separation are great, but they don't work very well for this task. What I want is something like a,b,c,d,e = *dict() a, b, c, d, e = [dict() for i in

Re: Syntactic sugar for assignment statements: one value to multiple targets?

2011-08-03 Thread Tim Chase
On 08/03/2011 03:25 AM, Steven D'Aprano wrote: gc wrote: Target lists using comma separation are great, but they don't work very well for this task. What I want is something like a,b,c,d,e = *dict() a, b, c, d, e = [dict() for i in range(5)] Unfortunately there is no way of doing so

Re: Syntactic sugar for assignment statements: one value to multiple targets?

2011-08-03 Thread Tim Chase
On 08/03/2011 03:36 AM, Katriel Cohn-Gordon wrote: On Wed, Aug 3, 2011 at 9:25 AM, Steven D'Aprano wrote: a, b, c, d, e = [dict() for i in range(5)] I think this is good code -- if you want five different dicts, then you should call dict five times. Otherwise Python will magically call your

Syntactic sugar for assignment statements: one value to multiple targets?

2011-08-02 Thread gc
Hi everyone! Longtime lurker, hardly an expert, but I've been using Python for various projects since 2007 and love it. I'm looking for either (A) suggestions on how to do a very common operation elegantly and Pythonically, or (B) input on whether my proposal is PEP-able, assuming there's no

Re: Syntactic sugar for assignment statements: one value to multiple targets?

2011-08-02 Thread Chris Angelico
On Wed, Aug 3, 2011 at 2:45 AM, gc gc1...@gmail.com wrote: Anyway, I frequently need to initialize several variables to the same value, as I'm sure many do. Sometimes the value is a constant, often zero; sometimes it's more particular, such as defaultdict(list). I use dict() below. If it's an