Multiple methods of passing function parameters in advanced python tutorials

  • 2020-04-02 14:03:04
  • OfStack

We've already touched on arguments for function. At that time, we passed the corresponding parameters according to the position. We will touch on more ways of passing parameters.

Recall the position transfer:


def f(a,b,c):
    return a+b+c print(f(1,2,3))

When f is called, 1, 2, and 3 are passed to a,b, and c, respectively, depending on the location.

Keyword passing

In some cases, passing by position can feel rigid. Keyword passing is the passing of a parameter based on the name of each parameter. Keywords are not used to correspond to the following locations. Still use the definition of f above, change the invocation mode:


print(f(c=3,b=2,a=1))

Keyword passing can be mixed with position passing. But the position parameter should appear before the keyword parameter:


print(f(1,c=3,b=2))

Parameter default

When defining a function, you can assign a default value to an argument by using the form a=19. If the parameter is not ultimately passed, the default value is used.


def f(a,b,c=10):
    return a+b+c print(f(3,2))
print(f(3,2,1))

On the first call to function f, we do not have enough values, c is not assigned, and c will use the default value of 10.

The second time the function is called, c is assigned to 1 and the default value is no longer used.

Parcel delivery

When defining a function, we sometimes don't know how many arguments will be passed when it is called. In this case, the packing position parameter, or the package keyword parameter, is very useful for parameter passing.

Here is an example of a parcel location transfer:


def func(*name):
    print type(name)
    print name func(1,4,6)
func(5,6,7,1,2,3)

Both calls, though with different Numbers of arguments, are based on the same func definition. In the parameter table of func, all the parameters are collected by name and merged into a tuple based on location, which is the package location pass.

To remind the Python parameter, name is the tuple name used to pass the package location and is preceded by a * when func is defined.

Here is an example of parcel keyword delivery:


def func(**dict):
    print type(dict)
    print dict func(a=1,b=9)
func(m=2,n=1,c=11)

Similar to the above example, dict is a dictionary that collects all the keywords and passes them to the function func. To remind Python, the dict parameter is the dictionary used to pass the package keyword, preceded by a **.

The key to package delivery is to define a function by adding a * or ** to the corresponding tuple or dictionary.

Solution package

* and **, which can also be used when called, unpacking, for example:


def func(a,b,c):
    print a,b,c args = (1,3,4)
func(*args)

In this case, unwrapping is passing a tuple so that each element of the tuple corresponds to a position parameter. Using * when calling func reminds Python that I want to break up args into three separate elements and pass them to a,b, and c. Imagine calling func without a * in front of args.

Correspondingly, there is also an unwrapping of the dictionary, using the same func definition, and then:


dict = {'a':1,'b':2,'c':3}
func(**dict)

When passing the dictionary dict, let each key-value pair of the dictionary be passed to func as a keyword.
 
hybrid

When defining or calling parameters, several ways of passing parameters can be mixed. But be careful about the sequence. The basic principle is that the location, then keyword, then wrap the location, then wrap the keyword, and according to the above said principle fine distinction.

Note: note the distinction between definition and invocation. Package and unpackage are not reverse operations, but two separate processes.

conclusion

Keyword, default value,

Package location, package keyword

Solution package


Related articles: