locals

Using locals() and format() method for strings: are there any caveats?

Using locals() and format() method for strings: are there any caveats? Question: Are there any disadvantages, caveats or bad practice warnings about using the following pattern? def buildString(user, name = ‘john’, age=22): userId = user.getUserId() return “Name: {name}, age: {age}, userid:{userId}”.format(**locals()) I had a very repetitive string generation code to write and was tempted to …

Total answers: 3

Python: load variables in a dict into namespace

Python: load variables in a dict into namespace Question: I want to use a bunch of local variables defined in a function, outside of the function. So I am passing x=locals() in the return value. How can I load all the variables defined in that dictionary into the namespace outside the function, so that instead …

Total answers: 7