SoFunction
Updated on 2024-11-16

Python dynamic parameters / namespaces / function nesting / global and nonlocal

1. Dynamic parameters of functions

 1.1 *args Dynamic passing of positional parameters

def chi(*food):
 print("I want to eat.", food)
chi("Rice.", "Millet.")
in the end:I want to eat. ('Rice', 'Millet Rice') # Multiple parameters passed in. The content received is the tupletuple

1.2 **kwargs Dynamic passing of keyword arguments

def func(**kwargs):
 print(kwargs)
func(a=1, b=2, c=3)
func(a=1, b=2)

Results.

{'a': 1, 'b': 2, 'c': 3}
{'a': 1, 'b': 2}

Order: positional arguments, *args, default, **kwarg

 def func(*args, **kwargs):
pass

Position of formal parameter *,**: aggregation

Position of real parameter*,**: break up

def fun(*args):
 print(args)
lst = [1, 4, 7]
fun(lst[0], lst[1], lst[2])
fun(*lst) # You can use * to break up a list into sequential order
s = "Short Legs Lupin 7."
fun(*s) # Strings can also be broken up, (iterable object (computing))

2. Namespaces

When the python interpreter starts executing, it opens up a space in memory, and whenever it encounters a variable, it records the relationship between the variable's name and its value, but when it encounters a function definition, the interpreter just reads the name of the function into memory to indicate that it exists, and it doesn't care about the variables and logic inside the function. In other words, at the beginning, the function is just loaded in, and that's it. It's only when the function is called and accessed that the interpreter opens up the internal space of the variables according to the variables declared inside the function. As the function finishes executing, the space occupied by the variables inside the function is cleared out as the function finishes executing.

2.1 Built-in Namespaces -- Stores the names provided by the python interpreter, lists, tuples, str, int, etc. are built-in namespaces.

2.2 Global namespaces -- Variables declared outside of a function, directly in the py file, belong to the global namespace.

2.3 Local namespaces -- Variables declared in a function are placed in a local namespace

Loading order: built-in namespaces >>> global namespaces >>> local namespaces (when the function is executed)

Order of values: local namespace >>> global namespace >>> built-in namespace

Scope.

Scope: Scope is the scope of action, according to the scope of effect is divided into global scope and local scope

Global scopes: contains built-in namespaces and global namespaces. They can be used anywhere in the entire file (following top-to-bottom line-by-line execution).

Local scopes: can be used inside functions.

1. Global scope: built-in + global
2. Local scope: local (function called)
3. globals() See what's in the globals
4. locals() View the contents of the current scope

a = 10
def func():
 a = 40
 b = 20
def abc():
 print("Ha ha.")
 print(a, b) # Local scopes are used here
 print(globals()) # Print the contents of the global scope
 print(locals()) # Print the contents of a local scope
func()

3. Nesting of functions

Functions can be nested within each other

def fun1():
 print(111)
def fun2():
 print(222)
 fun1()
fun2()
print(111)
# Nesting of functions
def fun2():
 print(222)
 def fun3():
  print(666)
 print(444)
 fun3()
 print(888)
print(33)
fun2()
print(555) 

4. Global and nonlocal key

global: local access to global content

a = 100
def func():
 global a # The addition of global means that the variable is no longer created locally. Instead, you use the global a
 a = 28
 print(a)
func()
print(a)

nonlocal: locally find the nearest variable in the outer function.

a = 10
def func1():
 a = 20
 def func2():
 nonlocal a
 a = 30
 print(a)
 func2()
 print(a)
func1()
# Results.
# Added nonlocal
# 30
# 30
# Without nonlocal
# 30
# 20

summarize

The above is a small introduction to Python dynamic parameters / namespaces / function nesting / global and nonlocal, I hope to help you, if you have any questions please leave me a message, I will promptly reply to you. I would also like to thank you very much for your support of my website!