Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!ecngs!feeder2.ecngs.de!newsfeed.freenet.ag!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.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'compiler': 0.05; 'cpython': 0.05; "'a'": 0.07; '*not*': 0.07; 'added.': 0.09; 'globals': 0.09; 'semantics': 0.09; 'symbols': 0.09; 'cc:addr :python-list': 0.10; 'def': 0.10; 'assume': 0.11; 'index': 0.13; 'approximates': 0.16; 'why,': 0.16; 'wrote:': 0.17; 'byte': 0.17; 'documented': 0.17; 'integer': 0.17; 'variables': 0.17; 'creates': 0.18; 'changes': 0.20; 'are.': 0.22; 'defined': 0.22; "i'd": 0.22; 'cc:2**0': 0.23; 'work.': 0.23; 'cc:no real name:2**0': 0.24; 'least': 0.25; 'cc:addr:python.org': 0.25; 'header:In-Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; 'am,': 0.27; 'rules': 0.27; 'functions.': 0.27; 'fixed': 0.28; 'chris': 0.28; 'run': 0.28; 'dictionary': 0.29; 'function': 0.30; 'code': 0.31; 'generally': 0.32; 'running': 0.32; 'print': 0.32; 'builds': 0.33; 'function.': 0.33; 'symbol': 0.33; 'list': 0.35; 'expected': 0.35; "won't": 0.35; 'explain': 0.36; 'but': 0.36; "i'll": 0.36; 'does': 0.37; 'subject:: ': 0.38; 'copying': 0.38; 'positive': 0.38; 'skip:l 20': 0.38; 'gives': 0.39; 'received:192': 0.39; 'where': 0.40; 'received:192.168': 0.40; 'different': 0.63; 'subject:...': 0.63; 'email addr:gmail.com': 0.63; 'header:Reply-To:1': 0.68; 'received:74.208': 0.71; 'reply-to:no real name:2**0': 0.72; 'execution.': 0.84; 'locals': 0.84; 'faster.': 0.91 Date: Wed, 04 Jul 2012 18:48:08 -0400 From: Dave Angel User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:13.0) Gecko/20120615 Thunderbird/13.0.1 MIME-Version: 1.0 To: eisoab@gmail.com Subject: Re: locals().update(...) References: <451cfda5-3fe6-4066-94f3-31f4fe747233@googlegroups.com> In-Reply-To: <451cfda5-3fe6-4066-94f3-31f4fe747233@googlegroups.com> Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-Provags-ID: V02:K0:mVYKwotpKkL4ic5YH3uAc/zoJlRsSKCDfFE9G9R6TYQ pgzxANeqtCScZ+yWLeQjT3s2H0exAemMw2nXDc/UZumNyllFk0 W9FAEu8Rkh7QpkIfB9A49x0kUSsL2movsqjiVwyAXNGtbx0cwy hFwGNs0vtY2RckHW9OppPpO1fyoLQ016Kn5Di+vPI8FLk3dyjJ vB1PzGQM5vFO86eBRp63fkbgfVdFgahuKdO+VNaLKHGzuazv7v 8iV14TKVn9Rocsxb+GMgsolDUIjkl8Ajtfm05Lett+mFFQoTeH lZ+pxBXZcx/bxNexj8XWx9DdYFxJdfv2DZiMwKN+6u2Sz+r0Q= = Cc: python-list@python.org X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list Reply-To: d@davea.name 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: 54 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1341442122 news.xs4all.nl 6906 [2001:888:2000:d::a6]:57675 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:24878 On 07/04/2012 07:56 AM, eisoab@gmail.com wrote: > I expected this to work: > > > def f(**args): > locals().update(args) > print locals() > print a > > d=dict(a=1) > > f(**d) > > but: >> global name 'a' is not defined > Where is my mistake? Chris has given you the place where it's documented that it generally won't work. I'll try to explain why, at least for local variables inside functions, and for CPython implementation. The generated byte code for a function does *not* look up each symbol by name during execution. The compiler builds a list of known local symbol names, and gives them each an index (a small positive integer). At run time that list is fixed in size and meaning; no new locals can be added. The locals() function just creates a dictionary that approximates what the semantics of the symbols are. But changes to that dictionary have no effect on the running function. That's one reason that copying a global value to a local symbol inside a function can make that function run faster. Globals always require a dictionary lookup, while function locals are just an integer offset away. > This does work: > > globals().update({'a':1}) > > print a > > 1 > > -E > As i said above, the rules are different for globals, and they are even if you use locals() to access them. However, i'd consider it prudent never to assume you can write to the dictionary constructed by either the locals() or the globals() functions. -- DaveA