Python parameter knowledge (meaning of adding a star before a variable)

Source: Internet
Author: User
Tags define function

Excessive Parameters

It is generally impossible to know the parameters of a function at runtime. Another scenario is that a function can operate many objects. What's more, calling your own functions becomes an api provided to available applications.

In these cases, python provides two special methods to define function parameters, allowing the function to accept excessive parameters without explicitly declaring parameters. These "extra" parameters are further explained.

Note that args and kwargs are just python conventions. You can name any function parameter in your favorite way, but it is better to be consistent with the standard usage of python so that your code can be easily understood by other programmers.

Location parameters

Use an asterisk before the parameter name to make the function accept any number of positional parameters.

>>> Def multiply (* args ):
... Total = 1
... For arg in args:
... Total * = arg
... Return total
...
>>> Multiply (2, 3)
6
>>> Multiply (2, 3, 4, 5, 6)
720

Python collects parameters into a single tuple as the variable args. Unless explicitly declared parameters, if there is no location parameter, this parameter serves as an empty tuples.

Keyword Parameter

Python uses two asterisks before the parameter name to support any number of keyword parameters.

>>> Def accept (** kwargs ):
... For keyword, value in kwargs. items ():
... Print "% s => % r" % (keyword, value)
...
>>> Accept (foo = 'bar', spam = 'eggs ')
Foo => 'bar'
Spam => 'eggs'

Note: kwargs is a normal python dictionary type, including parameter names and values. Without more keyword parameters, kwargs is an empty dictionary.

Mixed parameter type

Any location parameter and keyword parameter can be used with other standard parameter declarations. Be careful when using a mix, because their order in python is important. Parameters are classified into four categories, not all of which are required. They must be defined in the following order and skipped if not needed.

1) required parameters
2) optional parameters
3) Excessive location parameters
4) Excessive keyword Parameters

Def complex_function (a, B = None, * c, ** d ):

This order is required because * args and ** kwargs only accept any other parameters that are not included. Without this order, if you call a function with a location parameter, python does not know which value is required by the declared parameter or which is treated as an excessive parameter.

It should also be noted that when the function can accept many required parameters and optional parameters, it only needs to define an excessive parameter type.

Set of passing Parameters

In addition to functions that can accept any set of parameters, python code can also call functions with any number of functions, as mentioned earlier with asterisks. The parameters passed in this way are extended from python to the parameter list. Function to be called
You do not need to use excessive parameters for such calls. Any callable in python can be called using this technique. And use the same order rules with standard parameters.

>>> Def add (a, B, c ):
... Return a + B + c
...
>>> Add (1, 2, 3)
6
>>> Add (a = 4, B = 5, c = 6)
15
>>> Args = (2, 3)
>>> Add (1, * args)
6
>>> Kwargs = {'B': 8, 'C': 9}
>>> Add (a = 7, ** kwargs)
24
>>> Add (a = 7, * args)
Traceback (most recent call last ):
File "<stdin>", line 1, in <module>
TypeError: add () got multiple values for keyword argument 'A'
>>> Add (1, 2, a = 7)
Traceback (most recent call last ):
File "<stdin>", line 1, in <module>
TypeError: add () got multiple values for keyword argument 'A'

Pay attention to the last few lines in this example. Pay special attention to whether to explicitly pass keyword parameters when passing a tuple as an excessive location parameter. Because python uses order rules to extend excessive parameters, the positional parameters should be placed in front. In this example, the last two calls are the same. python cannot decide that the value is for.

Related Article

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.