python: How to use locals () and globals ()

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

Reference link

-12 Steps to Understand Python Decorators

Recommended Posts

python: How to use locals () and globals ()
How to install and use pandas_datareader [Python]
How to use Python zip and enumerate
How to use is and == in Python
python3: How to use bottle (2)
How to use Python argparse
[Python] How to use checkio
[Python] How to use hash function and tuple.
[Python] How to use input ()
How to use Python lambda
[Python] How to use virtualenv
python3: How to use bottle (3)
python3: How to use bottle
How to use Python bytes
How to install and use Tesseract-OCR
[Python] How to use Pandas Series
How to use Requests (Python Library)
How to use SQLite in Python
How to use .bash_profile and .bashrc
How to install and use Graphviz
[Python] How to use list 3 Added
How to use Mysql in python
How to use OpenPose's Python API
How to use ChemSpider in Python
How to use FTP with Python
Python: How to use pydub (playback)
How to use PubChem in Python
How to use python zip function
[Python] How to use Typetalk API
[Introduction to Udemy Python 3 + Application] 36. How to use In and Not
Comparison of how to use higher-order functions in Python 2 and 3
[Python] Summary of how to use pandas
How to package and distribute Python scripts
[Introduction to Python] How to use class in Python?
[python] How to use __command__, function explanation
[Python] How to use import sys sys.argv
[Python] Organizing how to use for statements
Memorandum on how to use gremlin python
[Python2.7] Summary of how to use unittest
How to use __slots__ in Python class
How to use "deque" for Python data
[Python] How to calculate MAE and RMSE
[Python] Understand how to use recursive functions
Summary of how to use Python list
How to use regular expressions in Python
[Python2.7] Summary of how to use subprocess
[Blender x Python] How to use modifiers
How to use pandas Timestamp and date_range
[Question] How to use plot_surface of python
How to use Serverless Framework & Python environment variables and manage stages
How to use Python with Jw_cad (Part 2 Command explanation and operation)
[Introduction to Python] How to use the Boolean operator (and ・ or ・ not)
How to use xml.etree.ElementTree
How to use Python-shell
How to use tf.data
How to use virtualenv
How to use Seaboan
How to use image-match
How to use shogun
How to install Python
How to use Virtualenv