1
player = 'Taro'
def f():
player = 'Jiro'
print('local:', locals())
f()
print(player)
Execution result of 1
local: {'player': 'Jiro'}
Taro
Without declaring local variables When you execute locals (),
2
player = 'Taro'
def f():
print('local:', locals())
f()
print(player)
Execution result of 2
local: {}
Taro
An empty dictionary is returned.
When I also run globals (),
3
player = 'Taro'
def f():
print('local:', locals())
f()
print('global:', globals())
Execution result of 3
local: {}
global: {'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <_frozen_importlib_external.SourceFileLoader object at 0x7efff584a2b0>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>, '__file__': 'Main.py', '__cached__': None, 'player': 'Taro', 'f': <function f at 0xxxxxxxxxxxxx>}
Many come out, 'player': It says'Taro'.
Besides, __name__
is __main__
,
__doc__
is None without anything in it
Something that has been declared in advance will come out.
If you write a document for this function here,
4
"""
test ##################
"""
player = 'Taro'
def f():
print('local:', locals())
f()
print('global:', globals())
Execution result of 4
local: {}
global: {'__name__': '__main__', '__doc__': '\ntest ##################\n', '__package__': None, '__loader__': <_frozen_importlib_external.SourceFileLoader object at 0x7f73121652b0>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>, '__file__': 'Main.py', '__cached__': None, 'player': 'Taro', 'f': <function f at 0x7f7312236e18>}
__doc__
is now'\ ntest ################## \ n.
If you also look at __name__
and __doc__
in the function,
5
player = 'Taro'
def f():
"""Test func doc"""
print(f.__name__)
print(f.__doc__)
f()
print('global:', __name__)
Execution result of 5
f
Test func doc
global: __main__
First,
print(f.__name__)
Outputs the name of the function f with
next,
The document of the function f is output by print (f.__doc__)
.
Finally,
With print ('global:', __name__)
The string'global:' is followed by the name of the entire function.
Recommended Posts