473,382 Members | 1,647 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Question about "exec in globals, locals"

I do not understand why the following code produces
NameError: name 'FirstClass' is not defined
when both a global and local dict are passed into exec, but
not when only a global dict is passed in.

I seek enlightenment!

Thanks,
Giles Brown

<python_code>

source = """
class FirstClass:
pass
class SecondClass:
References = [FirstClass]
"""

# Case - 1
myglobals = {'__builtins__' : None, '__name__': None}
exec source in myglobals

print "Global names are:", myglobals.keys()

# Case - 2
myglobals = {'__builtins__' : None, '__name__': None}
mylocals = {}

exec source in myglobals, mylocals

print "Global names are:", myglobals.keys()
print "Local names are:", mylocals.keys()

</python_code>
Jul 18 '05 #1
2 2370
Hello, Giles!
You wrote on 4 Jul 2003 01:41:36 -0700:

[Snipped]

GB> # Case - 2
GB> myglobals = {'__builtins__' : None, '__name__': None}
GB> mylocals = {}

GB> exec source in myglobals, mylocals
exec source in mylocals
exec source in myglobals
And it work with no errors. But I'm not sure you wish this.

GB> print "Global names are:", myglobals.keys()
GB> print "Local names are:", mylocals.keys()

With best regards, Egor Bolonev. E-mail: eb******@rol.ru [ru eo en]

Jul 18 '05 #2
Hi,
source = """
class FirstClass:
pass
class SecondClass:
References = [FirstClass]
"""

When you specify both locals() and globals(), here's what happen :

"""
class FirstClass:
pass

## ---------> Here, your locals() dict have been updated, but not your
## globals() one.
## But, here, you can do : References = [FirstClass] since your
## locals()
## know about 'FirstClass'

class SecondClass:
## ---------> Here, your locals() dict is a new one since you've
just
## entered a new scope. So, 'FirstClass' is neither defined in
## 'locals()' nor in 'globals()', that's why you have your
NameError
"""

I'm not quite sure of my explanation, but that could explain your
problem. Sorry if I've made any Python mistake.

Cheers,

--
Adrien Di Mascio
LOGILAB, Paris (France).
http://www.logilab.com http://www.logilab.fr http://www.logilab.org
Jul 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Mike | last post by:
Hi This will not work, does any one know why? exec("/usr/usr/bin -C $U2 &"); Thanks
5
by: Toby Donaldson | last post by:
Hi all, I'm designing an educational application that will run Python code and check the output against a pre-define answer. I want to use the "exec" statement to run the code, but I don't know...
2
by: tedsuzman | last post by:
----- def f(): ret = 2 exec "ret += 10" return ret print f() ----- The above prints '12', as expected. However,
1
by: Ted | last post by:
-------- def f(): ret = 2 exec "ret += 10" return ret print f() -------- The above example prints '12'. However, the following example prints
0
by: Jan | last post by:
I store sql-commands in a database table. In the first step I get the sql command out of the database table with embedded sql. In the second step I try to execute the command, which i got from the...
1
by: kurt.krueckeberg | last post by:
The second line of this script <?php // current directory echo getcwd() . "<br />"; print ( exec("ls *.*") ); ?> should display the names of the four files (it does in an ssh session) which...
21
by: comp.lang.tcl | last post by:
set php {<? print_r("Hello World"); ?>} puts $php; # PRINTS OUT <? print_r("Hello World"); ?> puts When I try this within TCL I get the following error:
2
by: xml0x1a | last post by:
How do I use exec? Python 2.4.3 ---- from math import * G = 1 def d(): L = 1 exec "def f(x): return L + log(G) " in globals(), locals() f(1)
0
by: =?ISO-8859-1?Q?Luis_M._Gonz=E1lez?= | last post by:
I apologize for this very basic question, but I can't understand how this works... I want to import a function from module B into my main script A, so this function can see and use the locals from...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.