Pythonz indefinite length in the form of parameters

Python title in the form of variable length parameters

In python, is usually a function with two parameters, a form of argument, referred to as parameter, parameter is also a parameter occurs when the function call, we usually referred to as the argument, and arguments can be passed in any value, then if the passed parameter is a tuple, or an ordered sequence, it can be called in python packets from the literal meaning can be understood as a package to a variable sequence, where the sequence can be multiple elements of. Meaning the packet is not to say, maybe longer direct parameter issues now.
The variable length parameter needs for improved scalability is a function of, for example, if a given function, find the product number n, then the general case, formal and actual parameters one to one, i.e. said n-th parameter corresponding to the n arguments, and the parameter type and argument types need to be consistent, otherwise an error function, it is generally suggestive of parameters do not match. For example, the following code, the code is wrong:
DEF Multiply (A, B):
the nums = A B
return the nums
Print (Multiply (3,4, 5))
Print (Multiply (3,4, 5))
error: TypeError : multiply () takes 2 positional arguments but 3 were given
either this argument at least one parameter
DEF Multiply (a, B):
the nums = a
B
return the nums
Print (Multiply (. 4))
given: TypeError: multiply () missing 1 required positional argument: 'b'
Is there a method parameter uncertainty, arguments casual way to achieve it, that is, to allow extension of this approach is better. Of course, the definition of a variable length parameter, then everything is solved.
Directly on the code:
DEF multiplyer ( args):
the nums =. 1
for in args I:
Print (type (args), args)
the nums the nums =
I
return the nums
#multiplyer (3,4, 5)
Print (multiplyer (33, 44) )
Print (multiplyer (33,44,55))
Print (multiplyer (33, 44, 'A'))

Some people may have doubts, why is the for loop it, because we are passing arguments into a tuple packet to the parameter, and in the for loop, we need to do is to arithmetic operations, and each cycle are we print what is the value of the cycle and the cycle of things, if you run this method, then you will find, print (type (args)) return to you is tuple, ie tuples. By then the value of the subscript alone, traversing the tuple these operations you can do that by operating this tuple, you can affect the parameter of this method, which is a variable length parameter of significance.

Published 13 original articles · won praise 0 · Views 308

Guess you like

Origin blog.csdn.net/alwaysbefine/article/details/104697361