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!