Hi !
In
Unifying types and classes in Python 2.2
article I see that: print a # show the result
{1: 3.25, 2: 200}
We can also use the new type in contexts where classic only allows
"real" dictionaries, such as the locals/globals dictionaries for the
exec statement or the built-in function eval():
print a.keys()
[1, 2] exec "x = 3; print x" in a
3
But I dont' understand that:
exec "x = 3; print x" in a
So what this code do ?
Why we need "in a" ?
This get same result !
Thanx for help:
FT 3 3925 ke*************@peto.hu wrote: But I dont' understand that: exec "x = 3; print x" in a
So what this code do ? Why we need "in a" ?
This get same result !
First we make sure there's no variable x in the global namespace: x
Traceback (most recent call last):
File "<stdin>", line 1, in ?
NameError: name 'x' is not defined
Underneath, the global namespace is just a dictionary with the variable
names as keys and the objects as values. "x" in globals()
False
Now let's use our own dictionary d as the namespace for the exec statement: d = {} exec "x=1" in d
This leaves the global namespace unaffected: "x" in globals()
False
Instead 1 is stored under the key "x" in the dictionary we provided: "x" in d
True d["x"]
1
Now let's repeat the same exec statement without explicitly providing a
dictionary. Python will then use globals() as the default. Therefore a
variable x with the value 1 will "magically" appear: exec "x=1" "x" in globals()
True x
1
Peter
On Tue, 29 Jun 2004, ke*************@peto.hu wrote: print a # show the result {1: 3.25, 2: 200} print a.keys() [1, 2] exec "x = 3; print x" in a 3
But I dont' understand that: exec "x = 3; print x" in a
So what this code do ? Why we need "in a" ?
The 'in a' tells exec to run the code using the dictionary a to read and
store variables. In this case, when x is set equal to 3, it's actually
a['x'] being set to 3. Try these examples to get an idea for what's going
on: a = {'x': 15} exec 'print x' in a
15 exec 'print x'
NameError: name 'x' is not defined exec 'x*=3; print x' in a
45 x
NameError: name 'x' is not defined a['x']
45 exec 'y=10; print y' in a
10 y
NameError: name 'y' is not defined a['y']
10
HI !
Thanx for every answer.
But it is a "bad" thing: when I think to I know something about python I
get some not understanded source code with helpful people's answers, and
them show me that I don't know nothing...
:-)
Christopher T King wrote: On Tue, 29 Jun 2004, ke*************@peto.hu wrote: >print a # show the result > > {1: 3.25, 2: 200}
>print a.keys() > > [1, 2]
>exec "x = 3; print x" in a > > 3
But I dont' understand that: exec "x = 3; print x" in a
So what this code do ? Why we need "in a" ?
The 'in a' tells exec to run the code using the dictionary a to read and store variables. In this case, when x is set equal to 3, it's actually a['x'] being set to 3. Try these examples to get an idea for what's going on: a = {'x': 15} exec 'print x' in a
15
exec 'print x'
NameError: name 'x' is not defined
exec 'x*=3; print x' in a
45
x
NameError: name 'x' is not defined
a['x']
45
exec 'y=10; print y' in a
10
y
NameError: name 'y' is not defined
a['y']
10 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Irmen de Jong |
last post by:
Hello,
I don't understand why the following doesn't work.
What I want to do is dynamically import some generated
Python code and I'm doing this using compile and exec'ing
it in the dict of a new...
|
by: Avi Kak |
last post by:
Hello:
I'd be most grateful if someone could answer
the following questions about the exec functions
in the os module.
1) How does one get one of the os.exec functions
in Python to execute a...
|
by: Andr? Roberge |
last post by:
I have the following two files:
#--testexec.py--
def exec_code(co):
try:
exec co
except:
print "error"
#-- test.py--
|
by: Antoon Pardon |
last post by:
I have the following little piece of code:
class Cfg:pass
#config = Cfg()
def assign():
setattr(config, 'Start' , )
def foo():
config = Cfg()
|
by: TPJ |
last post by:
I have the following code:
-----------------------------------
def f():
def g():
a = 'a' # marked line 1
exec 'a = "b"' in globals(), locals()
print "g: a =", a
|
by: Michael |
last post by:
Hi,
I'm having difficulty finding any previous discussion on this -- I
keep finding people either having problems calling os.exec(lepev), or
with using python's exec statement. Neither of...
|
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:
|
by: ...:::JA:::... |
last post by:
Hello,
After my program read and translate this code:
koristi os,sys;
ispisi 'bok kaj ima';
into the:
import os,sys;
|
by: Stef Mientki |
last post by:
Terry Reedy wrote:
sorry, don't know how this happened, as I always copy/paste ?
AFAIK locals() == sys._getframe(0).f_locals
AFAIK, again one level up
weird, I use it in 2.5 and if I remember...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
by: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
|
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...
| |