473,394 Members | 1,932 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,394 software developers and data experts.

Modify the local scope inside a function

Is there a way in python to add the items of a dictionary to the local
function scope? i.e. var_foo = dict['var_foo']. I don't know how many
items are in this dictionary, or what they are until runtime.

exec statements are difficult for debuggers to deal with, so as a
workaround I built my code into a function and saved it in a .py file.
The I load the .py file as a module and call the function instead. This
works great, and it has the added advantage of precompiled versions of
the code being saved as .pyc and .pyo files. (faster repeated
execution)

The only trouble was I execed inside a specially created scope
dictionary containing various variables and functions that the code
requires. I can't seem to figure out how to get this same effect inside
the function. Right now I'm passing the dict as an argument to the
function, but I can't modify locals() so it doesn't help me.

Thanks,
-Sandra

Feb 26 '06 #1
5 3181
Here you go. Unfortunate that you can't modify locals() easily, but
there are other options.

def foo(d):
for k in d:
exec '%s = %s' % (k, repr(d[k]))
print a + b

foo({'a':1, 'b':2})

Feb 26 '06 #2
Sandra-24 wrote:
Is there a way in python to add the items of a dictionary to the local
function scope? i.e. var_foo = dict['var_foo']. I don't know how many
items are in this dictionary, or what they are until runtime.


Why do you want to do this? Exec and eval should -not- be used for
this unless you are specifically creating a system allows arbitrary
code execution. What's wrong with using a dictionary? It's much safer
than allowing arbitrary names to be injected into a namespace.

Feb 26 '06 #3
On Sat, 25 Feb 2006 15:53:08 -0800, Sandra-24 wrote:
Is there a way in python to add the items of a dictionary to the local
function scope? i.e. var_foo = dict['var_foo']. I don't know how many
items are in this dictionary, or what they are until runtime.
Are you sure you need to do this? How do you use those variables later?

That is, I'm thinking that if you do this:

variables = {"var_foo"=0, "var_bar"=1, "var_baz"=3}
somehow_transfer_to_local_scope(variables)
print var_foo # or whatever...
print var_bar
print var_baz

it sort of looks pointless to me. In other words, if you have to deal with
the variables BY NAME in your code, then just define them by name. And if
you don't deal with them by name in your code, then they don't need names:

variables = {"var_foo"=0, "var_bar"=1, "var_baz"=3}
for item in variables.values():
print item
Okay, so they are *really* basic (and pointless) examples. But in most
cases that people say they want to turn strings into variables ("if I have
a string 'abc1', how do I turn it into a variable abc1?") it turns out
that doing so is not the best way of solving their problem.
exec statements are difficult for debuggers to deal with,
Which is one of the reasons why exec statements should be avoided whenever
possible. The rule of thumb I use is, any time I feel that I absolutely
must use exec, I drink tequila until the urge goes away.

*wink*
so as a
workaround I built my code into a function and saved it in a .py file.
That sounds like normal practice to me. Why aren't you doing that in the
first place?

The I load the .py file as a module and call the function instead. This
works great, and it has the added advantage of precompiled versions of
the code being saved as .pyc and .pyo files. (faster repeated
execution)
..pyc files don't give you faster execution on repeated calls. They give
you faster loading time on import. The benefit is on the first
import, not subsequent calls to the function. There may be other benefits
as well, but execution speed is not one of them.

Here is a test:

$ ls ftest*
ftest.py ftest.pyc
$ cat ftest.py
def f(): return sum(range(100))

(you'll just have to trust me that ftest.pyc is the compiled version of
ftest.py)

$ python /usr/lib/python2.3/timeit.py --setup="def f(): return sum(range(100))" "f()"
100000 loops, best of 3: 14 usec per loop

$ python /usr/lib/python2.3/timeit.py --setup="from ftest import f" "f()"
100000 loops, best of 3: 14.1 usec per loop

No appreciable difference in execution speed.

The only trouble was I execed inside a specially created scope
dictionary containing various variables and functions that the code
requires.
Ah, your exed'ed code is effectively using global variables.
I can't seem to figure out how to get this same effect inside
the function.
Define the names you need in the module.

var_foo = 0
var_bar = 1
var_baz = 3

def function(x):
return x + var_foo + var_bar + var_baz

Right now I'm passing the dict as an argument to the
function, but I can't modify locals() so it doesn't help me.


No, you can't modify locals. Locals returns a copy of the local
environment as a dict, but changing that dict doesn't change the local
variables.

There is an exception to that rule: if you run locals from the
interpreter, outside of a function, locals() is the same as globals():
locals() is globals() True

This means that this will work from the interpreter:
x = 6
locals()['x'] = -1
x

-1

but not anywhere else. That's a (rare) Python gotcha.

--
Steven.

Feb 26 '06 #4
Hey Crutcher, thanks for the code, that would work. I'm now debating
using that, or using function arguments to get the variables into the
namespace. This would require knowing the variables in the dict ahead
of time, but I suppose I can do that because it's part of the same
system that creates the dict. I'm just not very fond of having code
relating to one thing in more than one place, because it puts the onus
on the programmer to remember to change it in both places. Here I might
forgive it because it would make the generated code more readable.

It seems I created a fair amount of confusion over what I'm trying to
do. I use special psp like templates in my website. The template engine
was previously execing the generated template code. It uses special
environment variables that give it access to the functionality of the
web engine. These are what are in that scope dictionary of mine, and
why I exec the code in that scope.

However, I want to integrate a debugger with the web engine now, and
debugging execed generated code is a nightmare. So I save the generated
code as a function in a module that is generated by the template
engine. Unless I'm missing something about what you're saying, this
should now be faster as well, because afaik execed code has to be
compiled on the spot, wheras a module when you load it, is compiled (or
loaded from a .pyc file) at import time. So one import and repeated
function calls would be cheaper than repeated exec.

Thanks,
-Sandra

Feb 27 '06 #5
Hi Sandra,

Well, first, I'm not sure if you'd be interested, but Pydev Extensions
(http://www.fabioz.com/pydev) should be able to make remote debugging in
the way you want...Now, in order to do what you are trying to do,
debuggers (or at least the pydev debugger) go for the frame you want to
execute things (that contains the locals and globals in some scope).

In pydev extensions, in interactive debugging, the code to evaluate
expressions is something like:

frame = findFrame(thread_id, frame_id)
exec expression in frame.f_globals, frame.f_locals

So, you'd just need to get the frame... pydev does multithreaded
debugging, so, it needs to know the thread too, but if you just want to
debug the current thread, you could just go to curFrame =
sys._getframe() and then go iterating back in the frames to reach the
one you want at frame.f_back (that's basically what the findFrame
function does).

Cheers,

Fabio

Sandra-24 wrote:
Hey Crutcher, thanks for the code, that would work. I'm now debating
using that, or using function arguments to get the variables into the
namespace. This would require knowing the variables in the dict ahead
of time, but I suppose I can do that because it's part of the same
system that creates the dict. I'm just not very fond of having code
relating to one thing in more than one place, because it puts the onus
on the programmer to remember to change it in both places. Here I might
forgive it because it would make the generated code more readable.

It seems I created a fair amount of confusion over what I'm trying to
do. I use special psp like templates in my website. The template engine
was previously execing the generated template code. It uses special
environment variables that give it access to the functionality of the
web engine. These are what are in that scope dictionary of mine, and
why I exec the code in that scope.

However, I want to integrate a debugger with the web engine now, and
debugging execed generated code is a nightmare. So I save the generated
code as a function in a module that is generated by the template
engine. Unless I'm missing something about what you're saying, this
should now be faster as well, because afaik execed code has to be
compiled on the spot, wheras a module when you load it, is compiled (or
loaded from a .pyc file) at import time. So one import and repeated
function calls would be cheaper than repeated exec.

Thanks,
-Sandra

--
Fabio Zadrozny
------------------------------------------------------
Software Developer

ESSS - Engineering Simulation and Scientific Software
www.esss.com.br

Pydev Extensions
www.fabioz.com/pydev

PyDev - Python Development Enviroment for Eclipse
pydev.sf.net
pydev.blogspot.com
Mar 1 '06 #6

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

Similar topics

3
by: Robert Dodier | last post by:
Hello, I'm interested in introducing new variables into the environment of a Python interpreter or program. In reading through old posts to this newsgroup, I see there is an often-repeating...
1
by: Dave Theese | last post by:
Consider the following declaration *inside of a function*: int j(int); My compiler (VC++ 7.1) accepts this. typeid returns a type of int __cdecl(int). Local functions are not legal in C++....
4
by: Markus Dehmann | last post by:
I guess this is a kind of newbie question (since most pointer questions are newbie questions). In the program below, modify(string* s) is supposed to change the content that s points to. But the...
4
by: Peter Ammon | last post by:
I would like to share a variable between two functions defined in two separate source files; no other functions will need the global variable so I'd prefer to not give it file scope. Thus, I want...
8
by: TTroy | last post by:
I have a few questions about "scope" and "visibility," which seem like two different things. To me "visibility" of the name of a function or object is the actual code that can use it in an...
23
by: Timothy Madden | last post by:
Hello all. I program C++ since a lot of time now and I still don't know this simple thing: what's the problem with local functions so they are not part of C++ ? There surely are many people...
9
by: tai | last post by:
Hi. I'm looking for a way to define a function that's only effective inside specified function. Featurewise, here's what I want to do: bar_plugin_func = function() { ...; setTimeout(...);...
28
by: cpluslearn | last post by:
Hi, I have a local class inside a function template. I am able to wrap any type in my local class. Is it legal C++? What type of class is Local? Is it a class template or regular class? Thanks...
27
by: Erwin Moller | last post by:
Hi group, Consider this simple script (tested on FF3): <script type="text/javascript"> test = 'outer'; for (var i=0;i<2;i++){ alert(test); var test = 'inner'; alert (test);
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: 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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.