multiple values for keyword argument

Peter Otten __peter__ at web.de
Sat Jan 29 08:54:08 EST 2011


Tobias Blass wrote:

> 
> 
> On Sat, 29 Jan 2011, Francesco Bochicchio wrote:
> 
>>On 29 Gen, 12:10, Tobias Blass <tobiasbl... at gmx.net> wrote:
>>> Hi all
>>> I'm just learning python and use it to write a GUI (with Tkinter) for a
>>> C program I already wrote. When trying to execute the program below I
>>> get the following error message.
>>>
>>> Traceback (most recent call last):
>>> File "./abirechner.py", line 64, in <module>
>>> win =MainWin()
>>> File "./abirechner.py", line 43, in __init__
>>> self.create_edit(row=i);
>>> TypeError: create_edit() got multiple values for keyword argument 'row'
>>>
>>> I don't really understand why create_edit gets multiple values, it gets
>>> one Integer after another (as I see it)
>>> Thanks for your help

>>> class MainWin(Frame):
>>>     def create_edit(row,self):

>>Try this:
>>>         def create_edit(self, row):

> Ok it works now. So the problem was that python requires 'self' to be the
> first parameter?

When you invoke a method Python implicitly passes the instance as the first 
positional parameter to it, regardless of the name:

>>> class A:
...     s = "yadda"
...     def yadda(but_i_dont_want_to_call_it_self):
...             print but_i_dont_want_to_call_it_self.s
...
>>> A().yadda()
yadda

You can provoke the same error with a function:

>>> def f(a, b):
...     pass
...
>>> f(1, b=2)
>>> f(a=1, b=2)
>>> f(2, a=1)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: f() got multiple values for keyword argument 'a'

You can think of argument binding as a two-step process.
At first positionals are bound to the formal parameters in the order of 
appearance from left to right; then named arguments are bound to parameters 
with the same name. If a name is already catered by a positional argument 
(or a name doesn't occur at all and doesn't have a default value) you get an 
Exception.




More information about the Python-list mailing list