Two major classifications of function parameters
formal parameter Arguments written in parentheses in the function definition phase actual parameter Arguments passed in parentheses during function call phase
Relationship between formal and real parameters You can think of a formal parameter as a variable name,Real parameters are viewed as variable values Temporary binding during function call phase,Disconnect at the end of the function run Manifestations of Formal Reference variable name Real parameters are represented in a variety of ways (Get to the heart of it data value)
position parameter
position parameter Parameters to be filled in from left to right positional parameters Variable names filled in from left to right in the function definition phase positional real parameter Data values filled in from left to right in the function call phase
keyword real parameter(It's possible to break the order of positions) The function call phase passes the name of the formal parameter=Form of data values transmit a value 1.Position shapes and position real parameters in the function call phase,Binding by location 2.When binding position parameters, one more will not work and one less will not work. recount (e.g. results of election): The simpler the format, the higher the ranking,The more complex, the further back.
variable length parameter
1.Functions work fine no matter how many positional parameters are passed in variable length parameter def func(x,y,*a): print(x,y,a) func() # () func(1) # (1,) func(1, 2, 3, 4, 5, 6, 7) # (1, 2, 3, 4, 5, 6, 7) func(1,2) # 1 2 () func(1,2,3,4,5,6,7,8,9) # 1 2 (3, 4, 5, 6, 7, 8, 9) func(1,2) # 1 2 (3, 4, 5, 6, 7, 8, 9) *Used in formal parameters to receive redundant positional parameters and organized into tuples of rows assigned to the*trailing variable name
2.Functions work no matter how many keywords are passed into them def index(x, y, **b): print(x, y, b) index() # {} index(a=1,b=2,c=3,d=4) # {'a': 1, 'b': 2, 'c': 3, 'd': 4} index(y=2, x=1) # 1 2 {} index(y=2, x=1, u=222, k=111, l=444) # 1 2 {'u': 222, 'k': 111, 'l': 444} **Used in formal parameters to receive redundant keyword arguments and organized as a dictionary assigned to the**trailing variable name *utilization will list、Breaking up data within a tuple **utilization will break up the key-value pairs of the dictionary into keyword arguments passed into the
namespace
1.built-in namespace print() len() 2.global namespace pyCode written at the top of the document name = 'jason' # name global def func(): # func global pass if 1: a = 123 # a global for i in range(10): print(i) # i global while True: a = 123 # a global 3.Local namespace 函数体代码运行之后产生的都是Local namespace
summarize
That's all for this post, I hope it helped you and I hope you'll check back for more from me!