Tip: python function transfer is variable length

Source: Internet
Author: User

First come to Practice one:

def F1 (*args):    print(args) F1 (1,2,3,4): (1, 2, 3, 4)  ----- It's a Meta-ancestor.

Part 1:

The *args can pass any number of numbers, and args returns a primitive that can iterate

If you want to pass in a list or a meta-ancestor and change it into multiple arguments, let's say I entered [1,2,3,4] and passed into F1 to F1 (1,2,3,4) four parameters

def F1 (*args):print(args) F1 ([1,2,3,4])
draw: ([1,2,3,4],), If I do this is just a parameter, for args, just one element of the *args can be considered as a multi-parameter type, the Meta-ancestor split into a parameter, Just like the following print (*args): 1 2 3 4

If I pass a parameter to a function, this function passes the argument to another function, which can be used as above

def F1 (*args):    f2 (*args)        # so a,b,c,d will be assigned def  F2 (a,b,c,d) :    print(a,b,c,d) F1 ([1,2,3,4])

Part 2:

**args is a custom parameter value, args is the dictionary type, **args the args dictionary, if you want to pass two times the same group:

def f3 (** kwargs):    f2 (* *Kwargs)def  F2 (a,b,c,d    ): Print (a,b,c,d) f3 (a=1,b=2,c=3,d=4)

What the hell is *kwargs?
defF4 (*args,**kwargs):Print(args,kwargs) f4 (1,2,a=3,b=4 If such a function, well understood, the first one is Ganso ( the second one is the dictionary {'a': 3, 'b': 4} defF4 (*args,**kwargs):Print(*args,*Kwargs) First glance: wipe, this can be performed, but the result of this execution is:1 2a B comes in a three paragraph: * Take out Kwargs's Keykwargs is a dictionary also can say * You can remove the key from any dictionary. speculation: that is, any key can be*split, to perform one of the following:Print(*{'a': 3,'b': 4}) to conclude that a B means*similar operators, list to try:Print(*[1,2,3,4]) to draw 12 3 4string to Try:Print(*"FDFDSFD") that F D F D s f D can be extended to any iterator? indeed, no, * Only with strings, lists, dictionaries, collections, can only pass parameters in the function, the dictionary can only pass key, then I will pass the value of two * *

Tip: python function transfer is variable length

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.