modifying locals

J

John [H2O]

I would like to write a function to write variables to a file and modify a
few 'counters'. This is to replace multiple instances of identical code in a
module I am writing.

This is my approach:

def write_vars(D):
""" pass D=locals() to this function... """
for key in D.keys():
exec("%s = %s" % (key,D[key]))

outfile.write(...)
numcount += 1
do this, do that...

the issue is that at the end, I want to return outfile, numcount, etc... but
I would prefer to not return them explicitly, that is, I would just like
that the modified values are reflected in the script. How do I do this?
Using global? But that seems a bit dangerous since I am using exec.

Bringing up another matter... is there a better way to do this that doesn't
use exec?

Thanks!
 
S

Steven D'Aprano

I would like to write a function to write variables to a file and modify
a few 'counters'.

Are you talking about a function to generate Python source code?

This is to replace multiple instances of identical
code in a module I am writing.


Surely the right way to do this is to factor out the identical code into
a function, and then call the function.

This is my approach:

def write_vars(D):
""" pass D=locals() to this function... """
for key in D.keys():
exec("%s = %s" % (key,D[key]))

That would be better written as:

for key,item in D.iteritems():
exec "%s = %s" % (key, item)

exec is a statement, not a function, and doesn't require brackets.

outfile.write(...)
numcount += 1
do this, do that...

the issue is that at the end, I want to return outfile, numcount, etc...
but I would prefer to not return them explicitly, that is, I would just
like that the modified values are reflected in the script. How do I do
this? Using global? But that seems a bit dangerous since I am using
exec.

What you are actually trying to do is unclear to me. Perhaps you could
try explaining better with a more concrete example?

I wounder whether this might be what you are after?

# start of script (untested)
counter = 0 # define a counter in the module scope (global)
filename = 'foo.txt'

def make_vars():
global outfile, numcount # force names to be in module scope
outfile = open(filename, 'w')
numcount = 99

try:
numcount
except NameError:
print "numcount doesn't exist yet, making it"
make_vars()

print numcount
# end script


But of course the above can be written much more concisely as:

# start of script (untested)
counter = 0
filename = 'foo.txt'
outfile = open(filename, 'w')
numcount = 99
print numcount
# end script

so I'm not really sure you're trying to do what you seem to be doing.
 
J

John [H2O]

Steven said:
What you are actually trying to do is unclear to me. Perhaps you could
try explaining better with a more concrete example?

Actually, maybe a LACK of an example would make it simpler. What I'm after
is a function, to which I can pass a dictionary defined from locals(), then
in the function I would modify some of the variables from the dictionary.
But I want the modifications to be 'seen' by the method that called the
function without passing them back via return.

Ideally, I would prefer not to use global, as I think (due to other problem
in my scripting) this might cause problems.

Currently I these two possibilities:

def myFunction(D):
for key,item in D.iteritems():
exec "%s = %s" % (key, item)

modify a few of the elements...
return locals()

then,
#script
D=locals()
D=myFunction(D)
for key,item in D.iteritems():
exec "%s = %s" % (key,item)


OR:

def myFunction(D):
for key,item in D.iteritems():
exec "%s = %s" % (key, item)

modify a few of the elements...
declare global on the elements modified...


then,
#script
D=locals()
myFunction(D)

As a point.. I thought I read somewhere that D.iteritems wasn't going to be
available in Python3 so I've been trying to 'ween' myself from it.

Thanks!
 
M

Marc 'BlackJack' Rintsch

Actually, maybe a LACK of an example would make it simpler. What I'm
after is a function, to which I can pass a dictionary defined from
locals(), then in the function I would modify some of the variables from
the dictionary. But I want the modifications to be 'seen' by the method
that called the function without passing them back via return.

Why do you want that? That is typically something you don't want,
because it can make the program hard to understand very easily. Python
has no dynamic scoping and if you "hack" this into you program the
function call has very unexpected and surprising side effects.

So I ask for the use case too. What problem are you trying to solve?
There might be a better way than executing strings as code and trying to
inject names into the callers namespace.

Ciao,
Marc 'BlackJack' Rintsch
 
S

Steven D'Aprano

Also, locals() already returns a dict, no need for the exec trickery.
You can just modify it:
locals()["foo"]="bar"
foo
'bar'

That is incorrect. People often try modifying locals() in the global
scope, and then get bitten when it doesn't work in a function or class.

.... x = 1
.... locals()['y'] = 2
.... y
....Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 4, in foo
NameError: global name 'y' is not defined

You cannot modify locals() and have it work. The fact that it happens to
work when locals() == globals() is probably an accident.
 
A

Arnaud Delobelle

I would like to write a function to write variables to a file and modify a
few 'counters'. This is to replace multiple instances of identical code in a
module I am writing.

This is my approach:

def write_vars(D):
    """ pass D=locals() to this function... """
    for key in D.keys():
        exec("%s = %s" % (key,D[key]))

    outfile.write(...)
    numcount += 1
    do this, do that...

the issue is that at the end, I want to return outfile, numcount, etc... but
I would prefer to not return them explicitly, that is, I would just like
that the modified values are reflected in the script. How do I do this?
Using global? But that seems a bit dangerous since I am using exec.

Bringing up another matter... is there a better way to do this that doesn't
use exec?

What you're trying to do is hard to achieve but there's a reason for
that: it's a bad idea as it makes code really difficult to maintain.

You may want to define a class to contain all those variables that
need to be changed together:

class MyVars(object):
def __init__(self, outfile, numcount, ...):
self.outfile = outfile
self.numcount = numcount
def write_and_do_stuff(self):
self.outfile.write(...)
self.numcount += 1
# do this, do that...

Then you can use this in your functions:

def myfunction():
vars = MyVars(open('my.log', w), 0, ...)
# Do some stuff
vars.write_and_do_stuff()
# Do more stuff

You may even consider making some of those functions into methods of
the class.
 
M

M.-A. Lemburg

Hi,
Also, locals() already returns a dict, no need for the exec trickery.
You can just modify it:

locals()["foo"]="bar"
foo
'bar'

That is incorrect. People often try modifying locals() in the global
scope, and then get bitten when it doesn't work in a function or class.
def foo():
... x = 1
... locals()['y'] = 2
... y
...
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 4, in foo
NameError: global name 'y' is not defined

You cannot modify locals() and have it work. The fact that it happens
to work when locals() == globals() is probably an accident.

Ah thats interesting. I would not know because I usually avoid
such ugly hacks :)

It doesn't even work for already defined local variables:
.... x = 1
.... locals()['x'] = 2
.... print x
....1

The reason is that locals are copied in to a C array
when entering a function. Manipulations are then
done using the LOAD_FAST, STORE_FAST VM opcodes.

The locals() dictionary only shadows these locals: it copies
the current values from the C array into the frame's
f_locals dictionary and then returns the dictionary.

This also works the other way around, but only in very
cases:

* when running "from xyz import *"
* when running code using "exec"

globals() on the other hand usually refers to a module
namespace dictionary, for which there are no such
optimizations..

I don't know of any way to insert locals modified in
a calling stack frame... but then again: why would you
want to do this anyway ?

--
Marc-Andre Lemburg
eGenix.com

Professional Python Services directly from the Source (#1, Oct 31 2008)________________________________________________________________________

:::: Try mxODBC.Zope.DA for Windows,Linux,Solaris,MacOSX for free ! ::::


eGenix.com Software, Skills and Services GmbH Pastor-Loeh-Str.48
D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg
Registered at Amtsgericht Duesseldorf: HRB 46611
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,989
Messages
2,570,207
Members
46,782
Latest member
ThomasGex

Latest Threads

Top