Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.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; '*not*': 0.05; 'definitions': 0.07; 'function,': 0.07; 'received:verizon.net': 0.07; 'terry': 0.07; 'python': 0.08; '(string,': 0.09; 'definitions.': 0.09; 'dict': 0.09; 'dynamically': 0.09; 'exceptions': 0.09; 'executed': 0.09; 'generators': 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; 'subject:string': 0.09; 'exception': 0.12; 'def': 0.13; 'a,b,c': 0.16; 'couples': 0.16; 'definition,': 0.16; 'eliminated': 0.16; 'lexical': 0.16; 'namespace,': 0.16; 'namespace.': 0.16; 'parameter,': 0.16; 'reedy': 0.16; 'scope,': 0.16; 'subject:creation': 0.16; 'subject:variable': 0.16; 'survives': 0.16; 'values),': 0.16; 'syntax': 0.16; 'wrote:': 0.18; 'alternate': 0.18; 'functions,': 0.18; 'jan': 0.19; 'this?': 0.19; 'header:In-Reply-To:1': 0.22; 'dictionary': 0.23; 'function': 0.27; 'import': 0.27; 'script': 0.28; 'pm,': 0.29; 'definition': 0.30; 'usually': 0.31; 'does': 0.32; 'determined': 0.32; 'error.': 0.32; 'header:User-Agent:1': 0.33; 'header:X-Complaints-To:1': 0.33; 'there': 0.33; 'to:addr:python-list': 0.34; 'integer': 0.34; 'keys': 0.34; 'nested': 0.34; 'visible': 0.34; 'variables': 0.37; 'two': 0.37; 'passed': 0.37; 'received:org': 0.38; 'some': 0.38; 'subject:from': 0.38; 'e.g.': 0.39; 'to:addr:python.org': 0.40; 'within': 0.60; 'more': 0.61; 'body': 0.61; 'order': 0.62; 'stop': 0.63; 'here': 0.65; 'association': 0.66; 'guaranteed': 0.77; '12:09': 0.84; 'sum': 0.89 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Terry Reedy Subject: Re: Dynamic variable creation from string Date: Wed, 07 Dec 2011 17:59:33 -0500 References: 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:8.0) Gecko/20111105 Thunderbird/8.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: 44 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1323298798 news.xs4all.nl 6856 [2001:888:2000:d::a6]:35296 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:16793 On 12/7/2011 12:09 PM, Massi wrote: > in my script I have a dictionary whose items are couples in the form > (string, integer values), say > > D = {'a':1, 'b':2, 'c':3} > > This dictionary is passed to a function as a parameter, e.g. : > > def Sum(D) : > return D['a']+D['b']+D['c'] > > Is there a way to create three variables dynamically inside Sum in > order to re write the function like this? > > def Sum(D) : > # Here some magic to create a,b,c from D > return a+b+c No. The set of local names for a function is determined when the definition is executed and the body is compiled. Python 2 had an exception -- 'from x import *' -- that required an alternate complilation pathway. That possibility was eliminated in Python 3 and is now a syntax error. Within functions, 'locals().update(D)' is more or less guaranteed to *not* add local names to the local namespace, even if it does add keys to the locals() dict that shadows the local namespace. > It is really important that the scope of a,b,c is limited to the Sum > function, they must not exisit outside it or inside any other nested > functions. Local names, by definition, are lexically scoped to the function definition and are not visible without. Since nested definitions are part of that lexical scope, local names are always visible within nested definitions. You cannot stop that. The association of local names is usually dynamically limited to one function call. The two exceptions are enclosure by a nested function that survives the function call and generators in a paused state. -- Terry Jan Reedy