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 3937 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 empty module object.
That works okay, but as soon as the generated code
tries do perform certain imports, it fails!
Certain other imports succeed....
|
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 shell script that
includes some sort of a control structure in
the shell script itself?
|
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 which I mean here.
Just for a moment, let's just take one definition for one of the
|
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 well it already worked in 2.4.
but exchanging the function with the statement yields exactly the same
results
Yes ;-)
done.
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it. ...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in...
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |