[dgplug-users] Need Help in understanding the Error Message
Chaitanya Anil Kukde
chaitanya.kukde at gmail.com
Tue Feb 28 01:33:43 PST 2017
Hi Pabitra,
From what I understand, the function total is defined using positional
parameters, while 'name="Adi"' refers to usage of keyword arguments
which is (as I understand) not allowed and this is the cause of the
error. Relevant material which might help in understanding the
underlying mechanisms [0], [1].
The following modification in the code worked for me -
def total(*args, **kwargs):
if args:
print("%s has total money of Rs %d/- " %(kwargs['name'],
sum(args)))
else:
print("%s's piggy bank has no money" %kwargs['name'])
>>> total(*(3000, 10, 45), name="John Doe")
John Doe has total money of Rs 3055/-
Let me know your thoughts. Interesting question btw :)
0. https://docs.python.org/2/tutorial/controlflow.html#keyword-arguments
1. http://stackoverflow.com/a/1419159
Thanks,
Chaitanya
On 02/27/2017 03:07 PM, Pabitra Pati wrote:
> Hi All,
>
> I want to understand the error message I am getting.
> Below is my code piece :-
>
> def total(name, *args):
> if args:
> print("%s has total money of Rs %d/- " %(name, sum(args)))
> else:
> print("%s's piggy bank has no money" %name)
>
> I can call this method passing the extra arguments inside *().
> I know the correct way of passing the arguments. But, I am passing value
> for 'name' in form of param=value, *intentionally*, so that it throws me
> error. However, I am unable to understand the below error message :-
>
> >>> total(name="Adi", *(1, 2, 10) )
> Traceback (most recent call last):
> File "<stdin>", line 1, in <module>
> TypeError: total() got multiple values for keyword argument 'name'
>
> How Python is evaluating the above call, that it's getting multiple values
> for the parameter 'name'?
>
> Any help is appreciated.
>
>
> *Thanks,*
>
> *Pabitra*
> _______________________________________________
> Users mailing list
> Users at lists.dgplug.org
> http://lists.dgplug.org/listinfo.cgi/users-dgplug.org
More information about the Users
mailing list