Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #10602

Re: eval, exec and execfile dilemma

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!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <gandalf@shopzeus.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.040
X-Spam-Evidence '*H*': 0.92; '*S*': 0.00; '(at': 0.03; 'compiler': 0.07; 'exec': 0.07; 'prints': 0.07; 'globals': 0.09; 'namespace': 0.09; 'referenced': 0.09; 'namespace.': 0.16; 'parameter,': 0.16; 'program...': 0.16; 'help.': 0.19; 'variable': 0.21; 'sound': 0.22; "doesn't": 0.22; 'header:In-Reply-To:1': 0.22; 'restrict': 0.23; 'function': 0.26; 'skip:b 20': 0.26; "i'm": 0.27; 'sorry,': 0.29; 'module': 0.30; '"in': 0.30; 'version': 0.30; 'least': 0.31; 'print': 0.32; 'does': 0.32; 'to:addr:python-list': 0.34; 'header :User-Agent:1': 0.34; 'however,': 0.34; 'force': 0.34; "can't": 0.34; 'assignment': 0.35; 'function.': 0.35; 'visible': 0.35; 'installed': 0.35; 'thank': 0.35; 'but': 0.37; 'received:192': 0.38; 'subject:: ': 0.38; 'think': 0.38; 'got': 0.39; 'to:addr:python.org': 0.39; 'did': 0.40; 'your': 0.60; 'received:62': 0.67; 'works,': 0.68; 'discovered': 0.69; 'specialized': 0.71; '100': 0.72; 'locals': 0.84; 'received:192.168.0.100': 0.84; 'received:192.168.13': 0.84; 'received:62.179': 0.84; 'received:62.179.121': 0.84; 'received:upcmail.net': 0.84; '-->': 0.91
X-SourceIP 89.132.76.204
Date Sun, 31 Jul 2011 00:18:51 +0200
From Laszlo Nagy <gandalf@shopzeus.com>
User-Agent Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.18) Gecko/20110617 Thunderbird/3.1.11
MIME-Version 1.0
To python-list@python.org
Subject Re: eval, exec and execfile dilemma
References <4E346028.5000302@shopzeus.com> <CAPTjJmpitXU6mFH1C32=f8CSzL-oDZpx-jNxb0UZAcCZqDtq8A@mail.gmail.com> <4E34761C.7030609@stoneleaf.us>
In-Reply-To <4E34761C.7030609@stoneleaf.us>
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
X-Cloudmark-Analysis v=1.1 cv=zlRBWuFCZaNL9+WHNm1pWLowY5Lx061w2zJBJiDkNAU= c=1 sm=0 a=869cbtyjxWwA:10 a=y3nyYLdweaIA:10 a=RSPf8KcgRHcA:10 a=8nJEP1OIZ-IA:10 a=ui-CF7pVqbpt-yn7fLQA:9 a=wPNLvfGTeEIA:10 a=HpAAvcLHHh0Zw7uRqdWCyQ==:117
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.1677.1312064338.1164.python-list@python.org> (permalink)
Lines 38
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1312064338 news.xs4all.nl 23863 [2001:888:2000:d::a6]:44184
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:10602

Show key headers only | View raw


>> UnboundLocalError: local variable 'bar' referenced before assignment
>
> This works, though (at least it does on 2.7):
>
> --> exec "def foo():\n\tglobal bar\n\tbar+=1\n\treturn 1\n"
> --> bar = 9
> --> foo()
> 1
> --> bar
> 10
>
> Laszlo, why do you think you can't use exec?
I'm sorry, first I was not aware of the "in globals locals" part.

Then I also got an UnboundLocalError. Then I also figured out that I can 
use "global bar", but I did not want to. Reason: 100 functions/second 
installed to global namespace doesn't sound well. However, now I 
discovered the locals parameter, and I figured out that I can force the 
compiler to treat the name of the function to be a local name. I do this 
by reserving its name in locals. Then the compiler has no choice but to 
place it in the local namespace:

locals = {'_f_':None}
globals ={} # More specialized version in my program...
exec "def _f_(a):\n\treturn a+1\n\n" in globals,locals
print locals['_f_'](4) # prints '5'

This is good because it is not interfering with module level globals(), 
nor the local namespace. Also good because I can restrict what is 
visible from inside the function.

Thank you for your help.

Best,

    Laszlo

Back to comp.lang.python | Previous | NextNext in thread | Find similar | Unroll thread


Thread

Re: eval, exec and execfile dilemma Laszlo Nagy <gandalf@shopzeus.com> - 2011-07-31 00:18 +0200
  Re: eval, exec and execfile dilemma Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-07-31 09:31 +1000
    Re: eval, exec and execfile dilemma Laszlo Nagy <gandalf@shopzeus.com> - 2011-07-31 12:21 +0200

csiph-web