Python Basics 6-parameters of a function

Source: Internet
Author: User

form participates in the actual parameter
Formal parameters: In the function definition phase, the parameter defined in parentheses is called a formal parameter, which is equivalent to the variable name
Arguments: In the function call phase, parameters defined in parentheses are called arguments, which is equivalent to the value of a variable
That is, the parameter is the variable name, the argument is the value of the variable, when the function is called, the value is bound to the variable name, the function call ends, unbind
The difference:
Argument: is a variable that takes up memory space, data is routed one-way, arguments are passed to formal parameters, no parameters are passed to arguments
Formal parameters: virtual, no memory space, parametric only allocated memory units when called

Specific application:
#1, positional parameters: parameters defined in left-to-right order
Positional parameters: Required parameter
Positional arguments: Pass values to parameters by location
#2, keyword parameter: an argument that is defined in the form of Key=value
No need to pass values by location as parameter
Note the issue:
1. Keyword arguments must be to the right of the location argument
2. Cannot repeat a value for the same parameter
#3, default parameter: Parameters have been assigned values when they are defined
It is possible to pass a value or not to pass a value, often need to become a parameter defined as a positional parameter, the change of the smaller parameter is defined as the default parameter (formal parameter)
Note the issue:
1. Assign a value only once at the time of definition
2. The definition of the default parameter should be to the right of the location parameter
3. Default parameters should usually be defined as immutable types
#4, variable length parameters:
Variable length refers to the number of actual parameter values is not fixed
While the arguments are defined by position and by keyword, for the two forms of variable length, the formal parameter corresponding to have two solutions to completely store them, respectively, *args,*args no key value, **kwargs has a key value.

Example 1:
def test (x, y): #括号里面的就是位置形参
Print (' This x:%s '%x)
Print (' This y:%s '%y)
Test (#再调用时向函数的位置形参传递的位置实参)

Example 2:
def foo (x, y): #定义形参
Print (x, y)
Foo (1,y=2) #这里在调用时1赋值给x这个是位置形参, y=2 is the keyword parameter, then the result is printing 1 and 2, if, if Foo (y=2,1) write so is wrong, because X is not a value

Example 3:
def foo (x=1,y=2): #在定义函数阶段, has been assigned to a parameter, has been assigned in the definition phase, meaning that in the call phase
Can not pass value
Print (x, y)
Foo (x=10) #如果在调用时使用关键字参数或者位置参数, then the real arguments cover parameter

Example 4:
*args Example:
def func (X,y,z,*args):
Print (x, y, z)
Print (args)
Func (1,2,3,4,5,6,) #按照位置将实参传递给函数形参, when arguments passed by argument overflow, the remaining arguments are passed to the *args parameter
Func (1,2,3,*[4,5,6]) #传递给 *args's arguments are presented in tuples
Func (*[1,2,3,4,5,6]) #这个传参方式与func (1,2,3,4,5,6,) consistent
Func ([1,2,3,4,5,6],1,2,3,4) #列表内的数值代表形参的第一位, etc.

def func (x, Y, z):
Print (x, y, z)
l=[1,2,3]
Func (*l)
#这里实参传递与上述func (*[1,2,3,4,5,6]) method consistent

**kwargs Example:
def func (X,y,**kwargs):
Print (x, y)
Print (Kwargs)
Func (y=2,x=1,z=3,a=1,b=2) #根据关键字参数传递x, y, passes all parameters that are not defined in the parameter to **kwargs, and presents it as a dictionary
Func (1,2,3,z=3,a=1,b=2) #这个调用方式是错误的, where arguments 1 and 2 are passed to the parameter according to positional parameters, but argument 3 does not correspond to a formal parameter
Func (y=1,x=2,**{' a ': 1, ' B ': 2, ' C ': 3}) consistent #这个方法与func (y=2,x=1,z=3,a=1,b=2) results
Func (**{' x ': 1, ' A ': 1, ' B ': 2, ' C ': 3}) #这个调用方法是错误的, the calling method simply passes the value to **kwargs, and the x and Y parameters have no value to pass

def foo (x, Y, z):
Print (x, y, z)
dic={' x ': 1, ' Y ': 3, ' Z ': 1}
Foo (**dic)
#foo (x=1,y=3,a=1)

Example 5:
def home (Name,age,sex):
Print (' From home====> ', name,age,sex)
def wrapper (*args,**kwargs): #args = (1,2,3,4,5,6,7), kwargs={' C ': 3, ' B ': 2, ' A ': 1}
Home (*args,**kwargs)
#home (* (1,2,3,4,5,6,7), **{' C ': 3, ' B ': 2, ' A ': 1})
#home (1,2,3,4,5,7,a=1,b=2,c=3)
# Wrapper (1,2,3,4,5,6,7,a=1,b=2,c=3)
Wrapper (' Egon ', sex= ' male ', age=19)


Named keyword parameters (learn):
# parameter, the parameters defined after * are called named keyword parameters,
# It is characterized by a value that must be passed in the form of a keyword argument
# def foo (x,y=20,*args,a=1,b):
# Print (X,Y,A,B)

# foo (10,b=3)
# foo (10,22,33,44,a=2,b=3)

# positional parameters, default parameters, *args, named keyword parameters, **kwargs

Python Basics 6-parameters of a function

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.