Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'value,': 0.04; 'function,': 0.07; 'initialize': 0.07; '(it': 0.09; 'from:addr:python': 0.09; 'none:': 0.09; 'am,': 0.12; 'def': 0.15; 'class,': 0.15; "(i'd": 0.16; 'from:addr:mrabarnett.plus.com': 0.16; 'from:name:mrab': 0.16; 'message-id:@mrabarnett.plus.com': 0.16; 'objects.)': 0.16; 'operator.': 0.16; 'received:84.92': 0.16; 'received:84.92.122': 0.16; 'received:84.92.122.60': 0.16; 'reply-to:addr:python-list': 0.16; 'rhs': 0.16; 'subject: \n ': 0.16; 'unpack': 0.16; 'unpacking': 0.16; 'syntax': 0.16; 'wrote:': 0.16; 'defined': 0.19; 'maybe': 0.21; 'header:In-Reply-To:1': 0.22; '(or': 0.23; 'etc,': 0.23; 'aug': 0.24; 'times,': 0.24; 'statement': 0.25; 'guess': 0.26; 'separate': 0.28; 'received:84': 0.28; 'right.': 0.28; 'yield': 0.29; 'separately': 0.30; 'subject:?': 0.31; 'changing': 0.31; 'version': 0.32; 'chris': 0.32; 'minor': 0.32; 'proposed': 0.32; 'objects': 0.32; 'actually': 0.33; 'there': 0.33; 'to:addr:python-list': 0.33; 'instead': 0.33; 'header:User-Agent:1': 0.34; '17,': 0.34; 'assignment': 0.34; 'reply-to:addr:python.org': 0.34; 'surprised': 0.34; 'object': 0.35; 'issue': 0.36; 'convenient': 0.37; 'think': 0.38; 'should': 0.38; 'subject:: ': 0.39; 'either': 0.39; 'to:addr:python.org': 0.39; 'more': 0.60; '90%': 0.64; 'header :Reply-To:1': 0.71; 'reply-to:no real name:2**0': 0.71; 'subject:one': 0.77; 'dict()': 0.84; 'subject:value': 0.84 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AvYHAMnxS07Unw4S/2dsb2JhbABCmVuPFXeBQAEBBAE4QAYLCwgQCRYPCQMCAQIBDQI2EwgBAYdsArYxhkgEi19Ji3eLag Date: Wed, 17 Aug 2011 17:55:51 +0100 From: MRAB User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:5.0) Gecko/20110624 Thunderbird/5.0 MIME-Version: 1.0 To: python-list@python.org Subject: Re: Syntactic sugar for assignment statements: one value to multiple targets? References: <16ea4848-db0c-489a-968c-ca40700f5806@m5g2000prh.googlegroups.com> <7f30e39b-4e4f-4426-b819-b4670871d199@df3g2000vbb.googlegroups.com> In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list Reply-To: python-list@python.org List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 60 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1313600157 news.xs4all.nl 23970 [2001:888:2000:d::a6]:37369 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:11696 On 17/08/2011 10:26, gc wrote: > On Aug 17, 3:13 am, Chris Angelico 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]