LoginSignup
3
1

More than 5 years have passed since last update.

pythonの組込み関数locals()

Last updated at Posted at 2018-07-14

locals()はpythonの組み込み関数で,名前空間でローカルな変数を出力する機能があります.

def a():
    x = 1
    def b():
        x = 3
        print (x)
        print(locals())
    def c():
        print (x)
        print(locals())
    b()
    c()
    print (x)
    print(locals())

関数a()を実行すると以下のように出力されました.

3
{'x': 3}
1
{'x': 1}
1
{'c': <function a.<locals>.c at 0x7efd7c27eae8>, 'b': <function a.<locals>.b at 0x7efd7c27e9d8>, 'x': 1}

追記

@shiracamus 様に「vars()を引数無しで使えば同じ結果を返す」事をコメント頂きました.
ありがとうございました.

3
1
3

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
3
1