Learn Python's *args and **kwargs

Source: Internet
Author: User

*args represents any number of nameless parameters, which is a tuple (tuple); **kwargs represents a keyword argument, which is a dict (dictionary)

1 defFoo (*args, * *Kwargs):2     Print 'args =', args3     Print 'Kwargs =', Kwargs4     Print '###'5 if __name__=='__main__':6Foo (1,2,3,4)7Foo (a=1,b=2,c=3)8Foo (1,2,3,4, a=1,b=2,c=3)9Foo'a', 1, None, A=1, b='2', c=3)

As you can see from the running results above, these two are mutable parameters in Python.

Note: When using *args and **kwargs at the same time, you must *args the parameter column in front of the **kwargs, you can try to see the results

There's another usage, creating a dictionary.

1 def kw_dict (* *Kwargs):2     return  kwargs3print kw _dict (a=1,b=2,c=3)

Print it out:

1 {'a''b'c ': 3}

Learn Python's *args and **kwargs

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.