Python's multi-type pass-through and redundancy parameters

Source: Internet
Author: User

multi-type pass-through values (passing tuples and dictionaries to functions)1 passing tuples to a function
  def func (x, y    ): Print X+y
When we call this function, we only need to pass in two variables, but for example I have a tuple T = (), I want to pass the value of T to Func, then Func (t) is not possible, because by default it takes T as 1 variables, then in this case, we need to use * T to indicate that the data in the memory address of T is passed in, then it can be. However, it is important to note that the number of elements in the tuple T is less than the number of parameters defined in the function, when the function requires 3 parameters, there are only two elements in T, then the pass is OK, but to manually specify a parameter can be called normally:
def func (x, Y, z):     print x+y+= () func (1,*t)
This can be passed, and the *t of this notation, can only be placed on the far right (after the name of the parameter). you can also directly func (* ()) to pass the value2 Passing a dictionary to a functionIf you pass a dictionary, you need the following format
DIC = {'x': 2,'y': 3,'z': Ten} Func (**dic)
that's all you can do. But the key to the dictionary must be the same as the formal parameter of the function 3 Redundant parametersHandling redundant parameters (receiving redundant parameters)
def func (X,*args,**kwargs):
defined x named parameters, and two multi-type parameters, as redundant parametersby default, at least one named parameter is passed, and if there are many arguments, it is stored in args (in the form of a tuple), if the dictionary is passed, or the default (example X=1) value of the variable (in the form of a dictionary )
def func (x,*args,**Kwargs):  print  x  print  args  print c8> Kwargsfunc (1,2,a=123)1(2,) {'a': 123 }
 

Python's multi-type pass-through and redundancy parameters

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.