Python function argument type __ function

Source: Internet
Author: User

1. Required Parameters

2. Default parameters

3. Variable parameter (*var) var receives a tuple or list

*nums says that all the elements of the Nums list are passed in as variable parameters. This kind of writing is quite useful and very common.

When defining a variable parameter and defining a list or tuple parameter, only a * number is added to the parameter. Inside the function, the parameter numbers receives a tuple, so when you call the function, you can pass in any arguments, including 0 parameters:

4, named keyword parameter (*,varname1,)

5, the keyword parameter (**kw) KW received a dict

**extra said that extra this dict all key-value with keyword parameters passed to the function of the **kw parameter, KW will get a dict, note kw dict is a copy of the extra, the changes to KW will not affect the function of the extra



We can set the age and the city as the default parameter:

def enroll (name, Gender, age=6, city= ' Beijing '): Print ('
    name: ', name)
    print (' Gender: ', gender)
    print (' Age: ", age"
    print (' City: ', city)

In this way, most students do not need to provide age and city when registering, providing only two parameters that are required:

>>> enroll (' Sarah ', ' F ')
name:sarah
gender:f
age:6
city:beijing

Only students who are not in conformity with the default parameters need to provide additional information:

Enroll (' Bob ', ' m ', 7)
enroll (' Adam ', ' m ', city= ' Tianjin ')

As you can see, the default parameters reduce the difficulty of the function call, and when more complex calls are required, more arguments are passed to implement. Whether it's a simple call or a complex call, the function only needs to define one.

When multiple default parameters are invoked, the default parameters can be supplied in order, such as calling enroll (' Bob ', ' M ', 7), meaning that, in addition to the Name,gender parameters, the last 1 parameters are applied to the parameter age, and the city parameter is not provided. The default value is still used.

You can also provide partial default parameters in a sequential order. You need to write the parameter names when you do not supply part of the default parameters in order . For example, call Enroll (' Adam ', ' M ', city= ' Tianjin '), meaning that the city parameter uses the values passed in, and the other default parameters continue to use the default values.

Variable parameters can be directly passed in: Func (1, 2, 3), can be assembled first list or tuple, and then passed through the *args: Func (* (1, 2, 3));

Keyword parameters can be passed directly into: Func (A=1, b=2), can be assembled dict first, and then passed **kw to: func (**{' a ': 1, ' B ': 2})

For any function, it can be invoked in the form of a similar func (*args, **kw), regardless of how its arguments are defined.

You define functions in Python by using required parameters, default parameters, variable parameters, keyword parameters, and named keyword parameters, all of which can be combined in a combination of 5 parameters. Note, however, that the order of the parameter definitions must be: required, default, variable, named keyword, and keyword parameters.

Arrange order from front to back

Https://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000/ 001431752945034eb82ac80a3e64b9bb4929b16eeed1eb9000

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.