Key parameters of Python Functions

Source: Internet
Author: User

A friend asked me the following questions:

I have some minor questions when learning python. Could you please help me to answer:

I wrote a function in interactive mode:
Def adder (** ARGs ):
Sum = 0
For X in args. Keys ():
Sum + = ARGs [x]
Return sum

Then, enter adder ({'A': 1, 'B': 2, 'C': 3}) in interactive mode })
Why is the result not the sum I expected? But an error is reported. Traceback (most recent call last ):
File "<pyshell #22>", line 1, in
Adder ({'A': 1, 'B': 2, 'C': 3 })
Typeerror: adder () Takes exactly 0 arguments (1 given)

In my understanding, the ** ARGs parameters starting with the function adder should collect all the following parameters? I don't know where I got it wrong. Please help me. Thank you.

Solution:

Def adder (** ARGs ):
Print ARGs
Sum = 0
For X in args. Keys ():
Sum + = ARGs [x]
Return sum

Adder (a = 1, B = 2, c = 3)

Change to this.

As this friend said, ** ARGs parameters collect keyword parameters, but the passed function parameters should be similar to a = 1, rather than a dictionary, however, in a function, it can be seen as a dictionary, such as the output parameters of print args. We hope this example will help you understand the key parameters of Python functions.
If you want to know more about Python functions, you can look at the return values of Python functions.

ArticleLink: http://www.cnpythoner.com/post/pythonguanjian.html reprint please keep, thank you!

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.