Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!feeder.news-service.com!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!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.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'assign': 0.04; 'value,': 0.04; 'initialize': 0.07; 'option,': 0.07; 'immutable': 0.09; 'inclined': 0.09; 'syntax': 0.11; 'wed,': 0.12; 'am,': 0.13; 'wrote:': 0.15; '(multiple': 0.16; 'assignments,': 0.16; 'chained': 0.16; 'concise,': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'aug': 0.19; 'received:209.85.210.174': 0.19; 'received:mail- iy0-f174.google.com': 0.19; 'seems': 0.20; 'header:In-Reply-To:1': 0.22; 'dictionary': 0.23; "i'm": 0.27; 'message- id:@mail.gmail.com': 0.28; 'constant': 0.29; 'variables': 0.29; 'do.': 0.30; 'list:': 0.31; 'subject:?': 0.31; 'chris': 0.32; 'to:addr:python-list': 0.34; 'normally': 0.34; 'weird': 0.35; 'probably': 0.35; 'similar': 0.37; 'option': 0.37; 'but': 0.37; 'several': 0.37; 'received:google.com': 0.38; 'received:209.85': 0.38; 'subject:: ': 0.38; 'somewhat': 0.38; 'something': 0.38; 'think': 0.38; 'to:addr:python.org': 0.39; 'might': 0.39; 'received:209': 0.40; 'target': 0.61; 'below.': 0.65; 'subject:one': 0.77; 'dict()': 0.84; 'sides.': 0.84; 'subject:value': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; bh=sLJ0Ayh6ZpPFRR1YR4fQXCBUA27vEk2ZjFF1jcyfGZE=; b=SEgbOzpYx2s/xwyhaCZL+Pe5PU7lug5XQEXPHc0MXbBL89Pa0F9feVXvWQBOrt+k/Z ArYlOYRSRCSlsHIsrh0S1NEWCkXjayuaQWrNf9lwEdiX+EiKmliQNNWwk4g1or9I6Ci6 Mf07YembxGr5TDKXKU+4FFsgTSBV0pxq27TOk= MIME-Version: 1.0 In-Reply-To: <16ea4848-db0c-489a-968c-ca40700f5806@m5g2000prh.googlegroups.com> References: <16ea4848-db0c-489a-968c-ca40700f5806@m5g2000prh.googlegroups.com> Date: Wed, 3 Aug 2011 03:09:34 +0100 Subject: Re: Syntactic sugar for assignment statements: one value to multiple targets? From: Chris Angelico To: python-list@python.org Content-Type: text/plain; charset=ISO-8859-1 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: 29 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1312337377 news.xs4all.nl 23844 [2001:888:2000:d::a6]:58733 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:10777 On Wed, Aug 3, 2011 at 2:45 AM, gc 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 immutable value (such as a constant integer), you can use syntax similar to C's chained assignment: a=b=c=0 If you do this with dict(), though, it'll assign the same dictionary to each of them - not much use. > # Option 3 (multiple target list: this seems the most Pythonic, and is > normally what I use) > # Concise, separates variables from assignments, but somewhat > annoying; have to change individually and track numbers on both sides. > > a,b,c,d,e = dict(),dict(),dict(),dict(),dict() I think this is probably the best option, although I would be inclined to use dictionary-literal syntax: a,b,c,d,e = {},{},{},{},{} It might be possible to do something weird with map(), but I think it'll end up cleaner to do it this way. Chris Angelico