Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!feeder.news-service.com!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!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; 'does.': 0.07; 'function,': 0.07; 'received:verizon.net': 0.07; 'targets': 0.07; 'terry': 0.07; 'python': 0.08; '(possibly': 0.09; 'dict': 0.09; 'immutable': 0.09; 'mutable': 0.09; 'of)': 0.09; 'presume': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:80.91.229.12': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'received:lo.gmane.org': 0.09; 'tracker': 0.09; 'am,': 0.12; 'exception': 0.12; 'skip:[ 20': 0.12; 'case.': 0.15; "(i'd": 0.16; '*must*': 0.16; 'assignments,': 0.16; 'assignments.': 0.16; 'bindings': 0.16; 'bindings,': 0.16; 'doing),': 0.16; 'guessing': 0.16; 'iterator': 0.16; 'multiplies': 0.16; 'objects.)': 0.16; 'reedy': 0.16; 'subject: \n ': 0.16; 'unpack': 0.16; 'unpacking': 0.16; 'unpacking)': 0.16; 'syntax': 0.16; 'wrote:': 0.16; 'jan': 0.19; 'header:In-Reply-To:1': 0.22; 'implicit': 0.23; 'mask': 0.23; 'objects,': 0.23; 'changed': 0.24; 'times,': 0.24; 'code': 0.25; 'guess': 0.26; 'function': 0.27; 'skip:[ 10': 0.27; 'fact': 0.27; 'separate': 0.28; 'effect': 0.28; 'array': 0.30; '(so': 0.30; 'binding': 0.30; 'object.': 0.30; 'objects.': 0.30; "skip:' 10": 0.30; 'programmers': 0.31; 'subject:?': 0.31; 'seem': 0.31; 'changing': 0.31; 'version': 0.32; 'expression': 0.32; 'proposed': 0.32; 'generally': 0.32; 'objects': 0.32; 'does': 0.32; 'actually': 0.33; 'there': 0.33; 'to:addr:python-list': 0.33; 'instead': 0.33; 'header:User- Agent:1': 0.34; 'idea': 0.34; 'assignment': 0.34; 'keys': 0.34; 'latter': 0.34; 'sense,': 0.34; 'short,': 0.34; 'surprised': 0.34; 'pretty': 0.35; 'header:X-Complaints-To:1': 0.35; 'list.': 0.35; 'rather': 0.35; 'uses': 0.35; '(for': 0.35; 'object': 0.35; 'unless': 0.36; 'issue': 0.36; 'problems': 0.36; 'sequence': 0.37; 'thread': 0.37; 'but': 0.37; 'two': 0.37; 'think': 0.38; 'received:org': 0.38; 'some': 0.38; 'subject:: ': 0.39; 'header :Mime-Version:1': 0.39; 'either': 0.39; 'meaning': 0.39; 'to:addr:python.org': 0.39; 'case': 0.39; 'where': 0.40; 'more': 0.60; 'according': 0.62; '90%': 0.64; 'special': 0.67; 'match,': 0.73; 'subject:one': 0.77; 'dict()': 0.84; 'follow-on': 0.84; 'subject:value': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Terry Reedy Subject: Re: Syntactic sugar for assignment statements: one value to multiple targets? Date: Wed, 17 Aug 2011 12:12:47 -0400 References: <16ea4848-db0c-489a-968c-ca40700f5806@m5g2000prh.googlegroups.com> <7f30e39b-4e4f-4426-b819-b4670871d199@df3g2000vbb.googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: pool-74-109-121-73.phlapa.fios.verizon.net User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:6.0) Gecko/20110812 Thunderbird/6.0 In-Reply-To: X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list 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: 74 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1313597602 news.xs4all.nl 23903 [2001:888:2000:d::a6]:52738 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:11690 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 mutables but not for immutables. Python programmers must learn both this and the fact that Python does not make copies unless asked. Adding a special case exception to the latter to mask the former does not seem like a good idea. On 8/17/2011 5:26 AM, gc wrote: > It has only one very modest function, which is to unpack > > a, b, c, d, e = *dict() *expression has already been proposed to generally mean what it does in function calls -- unpack the iterator in place. funnylist = [1,2,*dict,99,100] # == [1,2]+list(dict)+[99,100] would interpolate the keys of the dict into the list. There is a tracker issue for this -- it would be a follow-on to the addition of *traget in assignments. In a real sense, "a,b = iterable" *already* means "a,b = *iterable". If *iterable had been in general use from the beginning, presume the latter is how we would write sequence unpacking for assignments. > a, b, c, d, e = dict(), dict(), dict(), dict(), dict() *expression will not be changed in meaning to magically re-evaluate an expression some multiple number of times according to code elsewhere. > 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(). Not 'duplicated', but 'bound'. > (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! Guessing that 90% of people are like you is likely to be wrong. I think this use case (for more than 2 or 3 copies) is pretty rare for most people. Where many people do trip up is "array = [[0]*i]*j", expecting to get j copies of [0]*i rather than j bindings of one object. But then, they must have the same wrong idea that [0]*i makes i copies of 0. For immutable 0, the misunderstanding does not matter. For mutable [0]*i, it does. People *must* learn that sequence multiplication multiplies bindings, not (copies of) objects. Both multiple copy problems have the same solution: array = [[0]*i for _ in range(j)] a,b,c,d,e = [dict() for _ in range(5)] The fact that the number of assignment sources (possibly after implicit unpacking) and targets have to match, unless one uses *target, and that both sides need to be changed if one is, is true of all assignments, not just this rare case. >--but changing either behavior would be a > very bad idea. This proposed syntax would be the Right Way to get > separate objects.) It would be very Wrong as it already has a very different meaning. -- Terry Jan Reedy