>>> # This area is the global or module scope
>>> number = 100
>>> def outer_func():
... # This block is the local scope of outer_func()
... # It's also the enclosing scope of inner_func()
... def inner_func():
... # This block is the local scope of inner_func()
... print(number)
...
... inner_func()
...
>>> outer_func()
100
def
or class
, respectivelymodules
only exist after you import them__dict__
ps1
which is defined in sys
sys.ps1 # .dot notation
sys.__dict__['ps1'] #subscription operation
lambda
expressionkeywords
, functions
, exceptions
, and other attributes that are built into PythonYou can use __code__
to examine the attributes of your code:
def square(base):
result = base ** 2
print(f'The square of {base} is: {result}')
>>> square.__code__.co_varnames
('base', 'result')
>>> square.__code__.co_argcount
1
>>> square.__code__.co_consts
(None, 2, 'The square of ', ' is: ')
>>> square.__code__.co_name
'square'
__main__
to hold the main program’s execution
__main__
is a special name reserved for the global scopedir()
to get the list of names in the global scope
>>> dir()
['__annotations__', '__builtins__',..., '__package__', '__spec__']
>>> var = 100 # Assign var at the top level of __main__
>>> dir()
['__annotations__', '__builtins__',..., '__package__', '__spec__', 'var']
global
statement