Python sequence split operator and map split operator examples

 

Use the sequence splitting operator (*) to provide positional arguments.

For example, the parameters of the function heron are stored in a list sides, which can be split: heron(sides[0], sides[1], sides[2]) or split: heron(*sides). If the list contains more items than the function arguments, you can use sharding to extract the appropriate arguments.

1. When using a function with a variable number of positional parameters , use

Sequence split operator (*) .

#1. At this point, args is a complete sequence, *args means to split all elements in a sequence as parameters of the function 
>>> def product(* args): result = 1 for arg in args: result *= arg return result #2. Therefore, when passing parameters, the split elements that belong to the same sequence should be passed >>> product(1,2,3,4 ) 24 >>> product(5,3 ) 15

You can put keyword arguments after positional arguments , i.e. def sum_of_power(*args,poewer=1):...

* itself as a parameter : indicates that no positional parameters should appear after *, but keyword parameters are allowed: def heron(a,b,c,*,unit="meters"):.....

If * is the first argument , no positional arguments are allowed, and keyword arguments are enforced when calling the function: def print_set(*,paper="Letter",copies=1,color=False):.. .. print_set() can be called with no arguments or to change some or all of the default values. But if you use positional parameters, a TypeError exception will be raised, such as print_set("A4") will produce an exception.

 

 2. When splitting the mapping, you can use

Map split operator (**)

For example use ** to pass a dictionary to the print_set() function:

 options = dict(paper="A4",color=True)

pritn_set(**options)

The dictionary's key-value pairs are split, and the value of each key is given the appropriate parameter with the same name as the key.

 Using ** in an argument creates a function that accepts any number of keyword arguments given:

>>> def add_person_details(ssn,surname,**kwargs):
      print ("SSN=",ssn)    print ("surname=",surname)    for key in sorted(kwargs):    print ("{0}={1}".format(key,kwargs[key])) >>> add_person_details(123,"Luce",forename="Lexis",age=47)   SSN= 123 surname= Luce age=47 forename=Lexis >>> add_person_details(123,"Luce") SSN= 123 surname= Luce

Example of sequence splitter and map splitter:

#Here, args is a sequence, * means to split args, so when *args is used as the first parameter of the function, it means that the position of the first parameter of print_args can 
# accept any number of elements (belonging to the same sequence), The same is true for the ** kwargs parameter
>>> def print_args(*args, ** kwargs): for i,arg in enumerate(args): print " positional argument{0}={1} " .format(i, arg) for key in kwargs: print " keyword argument{0}={1} " .format(key,kwargs[key]) >>> print_args( ' a ', ' b ', ' c ', ' d ', name= ' Tom ',age=12,sex='f')

result:

Mapping split example:

name="jack" age=24 s="name is {name} and age is {age}".format(**locals()) print s

result:

 

Reference: Mark Summerfield. "Python3 Program Development Guide". 149-151.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325118412&siteId=291194637