locals()
locals ()
returns all the values of variables in its local area in dictionary format.
def addspam(fn):
def new(*args):
print("spam. spam. spam")
print(locals())
return fn(*args)
return new
@addspam
def useful(a, b):
print(a**2 + b**2)
useful(3,4) # spam, spam, spam\n{'args': (3, 4)}
globals()
Returns a global variable in the same way
>>> y = 30
>>> globals()
{..., 'y': 30} #Other global variables automatically created by Python are displayed, but omitted
-12 Steps to Understand Python Decorators
Recommended Posts