15
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

python: locals()とglobals()の使い方

Last updated at Posted at 2016-02-10

locals()

locals()は自身のローカル領域の変数の値を全て辞書形式で返してくれる。

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()

も同様な形でグローバル変数を返してくれる

>>> y = 30
>>> globals()
{..., 'y': 30} #Pythonが自動的に作るグローバル変数が他にも表示されるが省略

参考リンク

15
9
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
15
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?