Special usage of star variable in python, python Asterisk
I. What is an asterisk variable?
Initially, the asterisk variable is used for parameter transfer of the function. In the following example, a single asterisk indicates that this position receives any number of non-Keyword parameters, convert the function * B to a metagroup, and the double star number indicates that this position receives any number of keyword parameters and converts it to a dictionary at ** B:
#! /Usr/bin/env python # coding = UTF-8 # -------- def one (a, * B): "" a is a common input parameter, * B is a non-Keyword asterisk parameter "print (B) one (1, 2, 3, 4, 5, 6) # -------- def two (a = 1, ** B ): "a is a common keyword parameter, ** B is a double star keyword parameter" print (B) two (a = 1, B = 2, c = 3, d = 4, e = 5, f = 6) # program output (2, 3, 4, 5, 6) {'B': 2, 'C': 3, 'E': 5, 'F': 6, 'D': 4} # From the output, in the first function, * multiple parameters without keywords can be input at the location of B. * B converts these input parameters into a tuples. The following calls one (1, 2, 3, 4, 5, 6) # After one (a, * B) is passed in, it is equivalent to one (1, (2, 3, 4, 5, 6) # In the second function, ** the location of B can receive any number of keyword parameters. The following calls two (a = 1, B = 2, c = 3, d = 4, e = 5, f = 6) # after one (a, * B) is passed in, it is equivalent to two (a = 1, {'B': 2, 'C': 3, 'E ': 5, 'F': 6, 'D': 4 })
After learning about the basic usage of a single asterisk and a double star, let's take a look at their extended usage.
Ii. single asterisk variable instance
A single asterisk variable can not only be used in parameter passing of functions, but actually uses a single asterisk prefix for a common variable to split the variable into a single element. See the following example:
#! /Usr/bin/env python # coding = UTF-8 # -------- def one (* x): "" output the first input parameter "print (x [0]) # -------- lst = ["a", "B", "c", "d"] stri = "www.qingsword.com" one (stri, lst) one (* lst) one (* stri) # program output www. qingsword. comaw # Call one (stri, lst) for the first time, which is equivalent to one (* x) (["a", "B", "c ", "d"], "www.qingsword.com") # The second call of one (* lst) is equivalent to one (* x) ("a", "B ", "c", "d") # The third call of one (* stri) is equivalent to one (* x) ("w", "w ", "w ",". "," q "," I "," n "," g "," s "," w "," o "," r "," d ",". "," c "," o "," m ") # If a single asterisk is used before a variable, it is actually a disassembly operation on the variable, separate the elements in the variable, and pass in one () function in sequence. After passing in one () function, one () the function will save these individual elements as a tuples, Which is why print (x [0]) can extract the first element.
To verify this, let's modify it.one()Function:
#! /Usr/bin/env python # coding = UTF-8 # -------- def one (* x): "" an incorrect instance. Try to modify the first input parameter value, exception thrown "print (x [0]) x [0] =" qingsword "lst = [" a "," B "," c ", "d"] one (* lst) # We know that the list can be changed. After splitting the list, we pass in the one () function and try to change the value of the first element inside the function, the result triggers a "TypeError" exception. You can try it by yourself. The reason for this result is described above. No matter what the original type of the input parameter is, one (* x) after receiving these input parameters at the position of * x, they will be saved as "tuples", and the tuples cannot be changed.
Let's take a look at several instances:
#! /Usr/bin/env python # coding = UTF-8 # -------- def one (* x): "print input parameters" for a in x: print () lst = ["abc", 123, "www.qingsword.com"] stri = "abcd" dect = {1: "one", 2: "two", 3: "three"} one (* lst) one (* stri) one (* dect) # The program outputs abc123www. qingsword. comabcd123 # the previous two calls are well understood. Finally, we passed in a dictionary element and found that only the keys of the dictionary elements are output, and no value is included. In fact, A single asterisk cannot read the value in the dictionary. It will always only read the key in the dictionary. to read the value in the dictionary, you must use the double star number.
Iii. binary star variable instance
At the end of section 2nd, we use a single asterisk to split a dictionary and pass it to the function, but we can only get the dictionary key. The following shows how to use a double star number to obtain the dictionary value:
#! /Usr/bin/env python # coding = UTF-8 # -------- def one (** x ): "Save the value of the input keyword parameter to the metagroup output" print (x) B = () for a in x. keys (): B + = (x [a],) print (B) dect = {"one": 1, "two": 2, "three ": 3} one (** dect) # program output {'three ': 3, 'one': 1, 'two': 2} (3, 1, 2) # using a double star prefix for a dictionary is equivalent to splitting it into keyword parameters. ** dect is equivalent to splitting the dictionary into one = 1, two = 2, three = 3 # pass the above keyword parameters to one (** x), which is equivalent to (remember what we mentioned earlier, double Star saves all the keyword parameters received into a dictionary.) one ({"one": 1, "two": 2, "three": 3 }) # Since it is a dictionary, all the methods in the dictionary can be used. Use the for loop to traverse the keys of this dictionary, then use a tuple to add the values corresponding to these keys, and finally print out the tuples.
Ps:Note: When using this method to pass a dictionary into a function, the dictionary key name must comply with the naming rules of python variables. It is not difficult to see through the above analysis, the double star number will first convert the dictionary into a keyword parameter, which is equivalent to using the key in the dictionary as the variable name. If the key does not comply with the variable naming rules, a "TypeError" exception will be thrown, you can try to upside down the keys and values in the dictionary above and use numbers as keys to see what problems may occur.
When "non-Keyword parameters (location parameters)" and "keyword Parameters" appear in the receiving parameters of a function, you can use a single asterisk to separate these two parameters. For example:
#! /Usr/bin/env python # coding = UTF-8 # -------- def mix (a, B, *, x, y ): "mixing location parameters with keyword Parameters" return a, B, x, y # The parameters a and B before the asterisks are location parameters, x and y after the asterisks are keyword parameters. when calling the mix () function and passing in parameters, the keyword parameters must be passed in as "variable name = value, if data is imported like a location parameter, a TypeError exception print (mix (, x = 3, y = 4) will be thrown) # program output (1, 2, 3, 4) # in the preceding mix function, if a single asterisk position parameter already exists between the location parameter and the keyword parameter, the parameter is followed by a keyword parameter, you do not need to use asterisks to separate them. For example #! /Usr/bin/env python # coding = UTF-8 # -------- def mix (a, B, * c, x, y ): "mixing location parameters with keyword Parameters" "return a, B, c, x, y # print (mix (, 5, x = 6, y = 7) of any position in * c # program output (1, 2, (3, 4, 5), 6, 7)
To include a combination of multiple parameters in a function, follow the following sequence:Location Parameter (required parameter), default parameter, single asterisk parameter or Asterisk separator, keyword parameter, double star parameter;
See the following example:
#! /Usr/bin/env python # coding = UTF-8 # -------- def mix (a, B = 0, * c, x, ** y ): "mixing location parameters with keyword Parameters" return a, B, c, x, yprint (mix (, 5, x = 6, y = 7, z = 8) # program output (1, 2, (3, 4, 5), 6, {'y': 7, 'z': 8 })
Summary
The above is all about this article. I hope it will be helpful for you to learn or use python. If you have any questions, please leave a message.