How to update local variables in function with a dictionary? - python

A problem meeted in rewriting a python program.
I move some previous global variables into a dictionay, so I have to rewrite the functions which have used those variables.
For example,
#old one
a, b = 1, 2
def func(c):
print a+b+c
#new one
d = dict(a=1,b=2)
def func(c,d):
a = d['a']
b = d['b']
print a+b+c
As the dictionary d is large, so I'm seeking a something like this
d = dict(a=1,b=2)
def func(c,d):
locals().update(d)
print a+b+c
I have tried __dict__.update(d), however __dict__ can't be accessed directly.
Or for key,value in d.items():
setattr(obj,key,value)
is possible? If yes, how to set the obj to the function itself?

You can't create local variables programmatically in that way.
Instead, just use the dictionary directly by accessing its keys.

What you could do instead is to evaluate your expression using the dictionary as the local variables:
def func(c,d):
print eval('a+b+c',globals(),dict(d,c=c))
func(3,dict(a=1,b=2))

You could use in clause of an exec statement. In the in clause you may provide namespace(s) for Python to search when executing a string. Documentation
For example:
>>> d = dict(a=1, b=2)
>>> def func(c,d):
exec 'print a+b+c' in d, locals()
>>> func(5,d)
8

Related

Declare multiple variable in a single line in python

I've code,
var a,b,c,d;
I need to rewrite this in python.
I'm not sure, since I'm new to python, how to define multiple variables in a sinlge line with no assignment.
I thought of doing
> a=None
> b=None
> c=None
> d=None
But it should be in one line
More pythonic way is tuple unpacking:
a, b, c, d = 1, 2, 3, 4
Or if you want to initialize to single value
a = b = c = d = 1
You could also use semi-colon (although not encouraged)
a=1; b=2; c=3; d=4
All of them would work.
You could use tuple unpacking:
a, b, c = 1, 2, 3
But to be honest, it would more Pythonic to do the assignments on separate lines.
The "to a single value" part warrants a little bit of extra explanation.
a = b = c = d = None
a = 1
print(b)
>>> None
The above statement does set the value of each variable to None, but with some other types of values, this may not always be the case. Python is an object-oriented language and if you replace the value None with another type of value that is a Python object you might get results that you don't expect (because Python sets all four variables to literally the same object). Consider:
a = b = c = d = list()
a.append(1)
print(b)
>>>[1]
The reason that the result is different is because a, b, c, and d are all referring to the same list. This is a fundamental concept in Python and a really important one to remember, and shows why making these types of one-line declarations can be potentially problematic.
As others have said, declaring your variables on the go (as you need them) is probably the better way to go overall, as it helps to avoid these types of "less obvious" declaration issues.
This will do it in a single line and several expressions
a=None; b=None; c=None; d=None
This will do it in a single line declaring all vars at once with the same value
a = b = c = d = None
And as pointed out in comments, you could even do it with 0 line since you can just use your vars on the go without prior declaration.

Python fstring as function

I'd like to use Python f-string for its syntactical simplicity, compared to string.Template() or other approach. However, in my application, the string is loaded from file, and the values of the variable can only be provided later.
If there a way to invoke fstring functionality separate from the string definition? Hopefully code below will better explain what I hope to achieve.
a = 5
s1 = f'a is {a}' # prints 'a is 5'
a = 5
s2 = 'a is {a}'
func(s2) # what should be func equivalent to fstring
By using eval() and passing either locals() or any arbitrary dict as the second positional locals argument, you can calculate an f-string dynamically on the fly with any combination of inputs.
def fstr(fstring_text, locals, globals=None):
"""
Dynamically evaluate the provided fstring_text
"""
locals = locals or {}
globals = globals or {}
ret_val = eval(f'f"{fstring_text}"', locals, globals)
return ret_val
Sample usage:
format_str = "{i}*{i}={i*i}"
i = 2
fstr(format_str, locals()) # "2*2=4"
i = 4
fstr(format_str, locals()) # "4*4=16"
fstr(format_str, {"i": 12}) # "10*10=100"
Use str.format().
Preferably, be explicit about passing arguments to it. But as a stopgap measure, you can use locals() to pass a dict of local (function-defined) variables to the formatting function:
foo = 'bar'
print('Foo is actually {foo}'.format(**locals()))
You can of course copy globals() to a local dict, and merge locals() to it, and use it to more closely emulate the f-string approach.
you can format it this way. pass in a dictionary of possible values for a and map it to your string.
dictionary = {
'a':[5,10,15]
}
def func(d):
for i in range(3):
print('a is {{a[{0}]}}'.format(i).format_map(d))
func(dictionary)
print:
a is 5
a is 10
a is 15
Here's what you were looking for:
pip install fstring
from fstring import fstring
x = 1
y = 2.0
plus_result = "3.0"
print fstring("{x}+{y}={plus_result}")
# Prints: 1+2.0=3.0
Here you go:
In [58]: from functools import partial
In [59]: def func(var_name, a):
...: return var_name + f' is {a}'
...:
In [60]: f = partial(func, 'a')
In [61]: f(5)
Out[61]: 'a is 5'
I am surprised why nobody answered with lambda:
foobar = lambda x: f"Foobar is {x}"
foobar(5)
> "Foobar is 5"

Python dynamic function attribute

I came across an interesting issue while trying to achieve dynamic sort.
Given the following code:
>>> l = []
>>> for i in range(2):
>>> def f():
>>> return f.v
>>> f.v = i
>>> l.append(f)
You have to be careful about how to use the functions in l:
>>> l[0]()
1
>>> l[1]()
1
>>> [h() for h in l]
[1, 1]
>>> [f() for f in l]
[0, 1]
>>> f = l[0]
>>> f()
0
>>> k = l[1]
>>> k()
0
>>> f = l[1]
>>> k()
1
>>> del f
>>> k()
NameError: global name 'f' is not defined
The behavior of the function depends on what f currently is.
What should I do to avoid this issue? How can I set a function attribute that does not depends on the function's name?
Update
Reading your comments and answers, here is my actual problem.
I have some data that I want to sort according to user input (so I don't know sorting criteria in advance). User can choose on which part of the data to apply successive sorts, and these sorts can be ascending or descending.
So my first try was to loop over the user inputs, define a function for each criterion, store this function in a list and then use this list for sorted's key like this: key=lambda x: [f(x) for f in functions]. To avoid multiplying conditions into functions themselves, I was computing some needed values before the function definition and binding them to the function (different functions with different pre-computed values).
While debugging, I understood that function attribute was not the solution here, so I indeed wrote a class with a __call__ method.
The issue is due to the fact that return f.v loads the global f, and not the one you intend.1 You can see this by disassembling the code:
>>> dis.dis(l[0])
3 0 LOAD_GLOBAL 0 (f)
3 LOAD_ATTR 1 (v)
6 RETURN_VALUE
After the loop that populates l, f is a reference to the last closure created, as you can see here:
>>> l
[<function f at 0x02594170>, <function f at 0x02594130>]
>>> f
<function f at 0x02594130>
Thus, when you call l[0](), it still loads the f that points to the last function created, and it returns 1. When you redefined f by doing f = l[0], then the global f now points to the first function.
What you seem to want is a function that has a state, which really is a class. You could therefore do something like this:
class MyFunction:
def __init__(self, v):
self.v = v
def __call__(self):
return self.v
l = [MyFunction(i) for i in range(2)]
l[0]() # 0
l[1]() # 1
Though it may be a good idea to explain your actual problem first, as there might be a better solution.
1: Why doesn't it load the global f and not the current instance, you may ask?
Recall that when you create a class, you need to pass a self argument, like so:
# ...
def my_method(self):
return self.value
self is actually a reference to the current instance of your object. That's how Python knows where to load the attribute value. It knows it has to look into the instance referenced by self. So when you do:
a.value = 1
a.my_method()
self is now a reference to a.
So when you do:
def f():
return f.v
There's no way for Python to know what f actually is. It's not a parameter, so it has to load it from elsewhere. In your case, it's loaded from the global variables.
Thus, when you do f.v = i, while you do set an attribute v for the instance of f, there's no way to know which instance you are referring to in the body of your function.
Note that what you are doing here:
def f():
return f.v
is not making a function which returns whatever its own v attribute is. It's returning whatever the f object's v attribute is. So it necessarily depends on the value of f. It's not that your v attribute "depends on the function's name". It really has nothing at all to do with the function's name.
Later, when you do
>>> f = l[0]
>>> k = l[1]
>>> k()
0
What you have done is bound k to the function at l[1]. When you call it, you of course get f.v, because that's what the function does.
But notice:
>>> k.v
1
>>> [h.v for h in l]
[0, 1]
So, a function is an object, and just like most objects, it can have attributes assigned to it (which you can access using dot notation, or the getattr() function, or inspecting the object's dictionary, etc.). But a function is not designed to access its own attributes from within its own code. For that, you want to use a class (as demonstrated by #VincentSavard).
In your particular case, the effect you seem to be after doesn't really need an "attribute" per se; you are apparently looking for a closure. You can implement a closure using a class, but a lighter-weight way is a nested function (one form of which is demonstrated by #TomKarzes; you could also use a named inner function instead of lambda).
Try this:
l = []
for i in range(2):
def f(n):
return lambda: n
l.append(f(i))
This doesn't use attributes, but creates a closure for each value of i. The value of n is then locked once f returns. Here's some sample output:
>>> [f() for f in l]
[0, 1]
As others said, return f.v looks for f name in the current scope which is equal to the last defined function.
To work around this you can simulate functions:
>>> class Function(object):
... def __init__(self, return_value):
... self.return_value = return_value
... def __call__(self):
... return self.return_value
...
>>> l = []
>>> for i in range(2):
... l.append(Function(i))
...
>>> l[0]()
>>> 0
>>> l[1]()
>>> 1

Changing a local variable in a function from another function

First, here's my example code:
EDIT: I should have specified, in my real code, that_func() is already returning another value, so I want it to return one value, and change c in addition
EDIT 2: Code edited to show what I mean
def this_func():
c=1 # I want to change this c
d=that_func()
print(c, d)
def that_func():
this_func.c=2 #Into this c, from this function
return(1000) #that_func should also return a value
this_func()
What I want to do is change the local variable c in this_func() to the value I assign it in that_func(), so that it prints 2 instead of 1.
From what I've gathered online, this_func.c=2 should do just that, but it doesn't work. Am I doing something wrong, or have I misunderstood?
Thanks for any and all help.
Yes, you misunderstood.
functions are not class. You can't access variables of a function like that.
Obviously, it's not the smartest of code that can be written, but this code should give an idea about how to use variables of a function.
def this_func():
c=1 # I want to change this c
c=that_func(c) # pass c as parameter and receive return value in c later
print(c)
def that_func(b): # receiving value of c from this_func()
b=2 # manipulating the value
return b #returning back to this_func()
this_func()
Wrap it in an object and pass it to that_func:
def this_func():
vars = {'c': 1}
d = that_func(vars)
print vars['c'], d
def that_func(vars):
vars['c'] = 2
return 1000
Alternatively, you can pass it in as a regular variable and that_func can return multiple values:
def this_func():
c = 1
c, d = that_func(c)
print c, d
def that_func(c):
c = 2
return c, 1000

defining variable from string

I'm trying to define variable inside function. vars() shows variable is created, but gives me NameError: exception. What am I doing wrong?
def a(str1):
vars() [str1] = 1
print vars()
print b
a('b')
output:
{'str1': 'b', 'b': 1}
exception:
NameError: global name 'b' is not defined
You're invoking undefined behaviour. From the documentation of vars():
Note The returned dictionary should not be modified: the effects on the corresponding symbol table are undefined.
Other answers give possible solutions.
Your code works for me. Perhaps you should try an alternative approach:
exec(str1 + '=1')
This will execute b=1
If you don't understand why a construct doesn't work, neither will the next person who has to read your code. If you mean
b = 1
you should say that. In this case vars() gives you access to the function local dictionary so your code is equivalent to
def a():
b = 1
where b is local to a and evaporates when it goes out of scope upon exit from a.
premature optimization is the root of many people's attempt to second-guess Python
from itertools import izip
import timeit
import msw.wordlist
def zip_list(p):
"""construct a dictionary of length 100 from a list of strings"""
return dict(zip(p[:100], p[100:]))
def izip_list(p):
"""as zip_list but avoids creating a new list to feed to dict()"""
return dict(izip(p[:100], p[100:]))
def pass_list(p):
"""take 100 elements of a list and do nothing"""
for i in p[:100]:
pass
def exec_pass_list(p):
"""exec() a no-op 100 times"""
for i in xrange(100):
exec('pass')
# returns a list of 64'000 unique lowercase dictionary words for tests
words = msw.wordlist.Wordlist()
words.shuffle()
words = words[:200]
print 'words', words[:5], '...'
for func in ['zip_list', 'izip_list', 'pass_list', 'exec_pass_list']:
t = timeit.Timer('%s(words)' % func,
'from __main__ import words, %s' % func)
print func, t.repeat(number=10**5)
which yields:
words ['concatenated', 'predicament', 'shtick', 'imagine', 'stationing'] ...
zip_list [1.8603439331054688, 1.8597819805145264, 1.8571949005126953]
izip_list [1.5500969886779785, 1.5501470565795898, 1.5536530017852783]
pass_list [0.26778006553649902, 0.26837801933288574, 0.26767921447753906]
exec_pass_list [74.459679126739502, 75.221366882324219, 77.538936853408813]
I didn't bother trying implement whatever the OP was trying to do because being 50 times slower to not construct a dictionary sort makes further testing kinda stupid.

Categories

Resources