Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > multiple values for keyword argument

Reply
Thread Tools

multiple values for keyword argument

 
 
Tobias Blass
Guest
Posts: n/a
 
      01-29-2011
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

abirechner.py:

# line 37
class MainWin(Frame):
def __init__(self,master=None):
Frame.__init__(self,master)
self.grid()
self.edits=()
for i in range(10):
self.create_edit(row=i);
def create_edit(row,self):
# LineEdit is defined, but I don't consider it important here
self.edits+=LineEdit()
self.edits[-1].grid(row=row,column=0)
# ...
#line 64
win = MainWin()
win.mainLoop()
 
Reply With Quote
 
 
 
 
Francesco Bochicchio
Guest
Posts: n/a
 
      01-29-2011
On 29 Gen, 12:10, Tobias Blass <tobiasbl...@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
>
> abirechner.py:
>
> # line 37
> class MainWin(Frame):
> * * * * def __init__(self,master=None):
> * * * * * * * * Frame.__init__(self,master)
> * * * * * * * * self.grid()
> * * * * * * * * self.edits=()
> * * * * * * * * for i in range(10):
> * * * * * * * * * * * * self.create_edit(row=i);
> * * * * def create_edit(row,self):
> * * * * * * * * # LineEdit is defined, but I don't consider it important here
> * * * * * * * * self.edits+=LineEdit()
> * * * * * * * * self.edits[-1].grid(row=row,column=0)
> # ...
> #line 64
> win = MainWin()
> win.mainLoop()


Try this:

> def create_edit(self, row):


Ciao
---
FB
 
Reply With Quote
 
 
 
 
Tobias Blass
Guest
Posts: n/a
 
      01-29-2011


On Sat, 29 Jan 2011, Francesco Bochicchio wrote:

>On 29 Gen, 12:10, Tobias Blass <tobiasbl...@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
>>
>> abirechner.py:
>>
>> # line 37
>> class MainWin(Frame):
>> * * * * def __init__(self,master=None):
>> * * * * * * * * Frame.__init__(self,master)
>> * * * * * * * * self.grid()
>> * * * * * * * * self.edits=()
>> * * * * * * * * for i in range(10):
>> * * * * * * * * * * * * self.create_edit(row=i);
>> * * * * def create_edit(row,self):
>> * * * * * * * * # LineEdit is defined, but I don't consider it important here
>> * * * * * * * * self.edits+=LineEdit()
>> * * * * * * * * self.edits[-1].grid(row=row,column=0)
>> # ...
>> #line 64
>> win = MainWin()
>> win.mainLoop()

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

>
>Ciao
>---
>FB
>


Ok it works now. So the problem was that python requires 'self' to be the first
parameter?
 
Reply With Quote
 
Frank Dierkes
Guest
Posts: n/a
 
      01-29-2011
On Sat, 29 Jan 2011 14:18:30 +0100, Tobias Blass wrote:

> On Sat, 29 Jan 2011, Francesco Bochicchio wrote:
>
>>> class MainWin(Frame):
>>> Â* Â* Â* Â* def create_edit(row,self):


>>> def create_edit(self, row):

>>


>>
>>

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


If you define an instance method, the first parameter is always the
instance passed to the method - regardless of the parameters name.

In your case the instance was passed to the row parameter. Then again you
wanted to pass i to it. That's why the exception was raised. If you just
had typed self.create_edit(i), then row would have been the instance
(*self*.create_edit(...)) and self would have been i.

Naming the first parameter self is only a convention. It could be any
other name, too.
 
Reply With Quote
 
Peter Otten
Guest
Posts: n/a
 
      01-29-2011
Tobias Blass wrote:

>
>
> On Sat, 29 Jan 2011, Francesco Bochicchio wrote:
>
>>On 29 Gen, 12:10, Tobias Blass <tobiasbl...@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.

 
Reply With Quote
 
Roy Smith
Guest
Posts: n/a
 
      01-29-2011
In article <>,
Frank Dierkes <> wrote:

> Naming the first parameter self is only a convention. It could be any
> other name, too.


But it shouldn't. The use of "self" is so universal that using anything
else will just make your code more difficult for other people to
understand.
 
Reply With Quote
 
Ethan Furman
Guest
Posts: n/a
 
      01-29-2011
Roy Smith wrote:
> In article <>,
> Frank Dierkes <> wrote:
>
>> Naming the first parameter self is only a convention. It could be any
>> other name, too.

>
> But it shouldn't. The use of "self" is so universal that using anything
> else will just make your code more difficult for other people to
> understand.


Nevertheless, if you have a good reason to, go ahead.

I, myself, use the spanish word 'yo' instead (less keystrokes, I hate
'self', and it amuses me); if I'm working with my numerical experiments
I'll use 'n' or 'x'... although, when posting sample code to c.l.py I do
try to use 'self' to avoid possible confusion.

~Ethan~
 
Reply With Quote
 
Tobias Blass
Guest
Posts: n/a
 
      01-29-2011


On Sat, 29 Jan 2011, Peter Otten wrote:

>Tobias Blass wrote:
>
>>
>>
>> On Sat, 29 Jan 2011, Francesco Bochicchio wrote:
>>
>>>On 29 Gen, 12:10, Tobias Blass <tobiasbl...@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.
>
>

Thanks for your replies, as soon as I knew that python always passes the object
reference as first parameter everything was clear (It's just like argc and argv
in C, you could also call argc fish and argv chips)
 
Reply With Quote
 
patty@cruzio.com
Guest
Posts: n/a
 
      01-29-2011
> Roy Smith wrote:
>> In article <>,
>> Frank Dierkes <> wrote:
>>
>>> Naming the first parameter self is only a convention. It could be any
>>> other name, too.

>>
>> But it shouldn't. The use of "self" is so universal that using anything
>> else will just make your code more difficult for other people to
>> understand.

>
> Nevertheless, if you have a good reason to, go ahead.
>
> I, myself, use the spanish word 'yo' instead (less keystrokes, I hate
> 'self', and it amuses me); if I'm working with my numerical experiments
> I'll use 'n' or 'x'... although, when posting sample code to c.l.py I do
> try to use 'self' to avoid possible confusion.
>
> ~Ethan~
> --
> http://mail.python.org/mailman/listinfo/python-list


I am glad you said this. I have been avoiding understanding this 'self',
just accepting it :} For the time being, since my programs I am creating
are for my own use, I think I will make my own names up, that are
descriptive to me as the programmer, it's all going to be interpreted
anyway. And the other email equating to C's argv, etc. - now I get it.

Regards,

Patty
>
>



 
Reply With Quote
 
Steven D'Aprano
Guest
Posts: n/a
 
      01-29-2011
On Sat, 29 Jan 2011 09:03:28 -0500, Roy Smith wrote:

> In article <>,
> Frank Dierkes <> wrote:
>
>> Naming the first parameter self is only a convention. It could be any
>> other name, too.

>
> But it shouldn't. The use of "self" is so universal that using anything
> else will just make your code more difficult for other people to
> understand.


It's a strong convention, true, but for Python to prohibit names other
than self would be a serious mistake. It would add complication to the
parser, for no real benefit. Remember, there's nothing special about
methods. They're just ordinary functions which happen to be passed an
extra argument at runtime. Making it compulsory to call the first
argument "self" would seriously cripple Python's usefulness in at least
three cases.

(1) Class methods receive the class, not the instance, and the convention
is to call that first argument "cls". It would be confusing and
misleading to call it "self".

Similarly, I sometimes use a similar descriptor which combines the
functionality of class methods and ordinary methods. Since neither "self"
nor "cls" would be appropriate, I use the name "this" for the first
argument:

http://code.activestate.com/recipes/...od-descriptor/

Descriptors are a general protocol, so there may be other such
technologies being used by people.

(2) Sometimes people use functions inside a class namespace as an
ordinary function, perhaps as a factory function, called at class
creation time. Here is a toy example that automates the building of
properties:

class K(object):
def build(name): # called at class creation time
def getter(self):
return getattr(self, "_" + name)
def setter(self, value):
setattr(self, "_" + name, value)
return property(getter, setter)
spam = build("spam")
ham = build("ham")


(3) Because methods are just a wrapper around an ordinary function, you
can inject almost any function into a class at runtime. You don't know
what the first argument of that function is called:

>>> class K(object):

.... def __init__(self, value='spam'):
.... self.attr = value
....
>>> def get_attr(obj):

.... return obj.attr
....
>>> k = K()
>>> get_attr(k)

'spam'
>>>
>>> K.method = get_attr
>>> k.method()

'spam'




--
Steven
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
RE: keyword checker - keyword.kwlist Hamilton, William Python 4 05-13-2007 06:31 AM
keyword checker - keyword.kwlist tom@finland.com Python 6 05-10-2007 04:53 PM
getting a class attribute using a keyword argument Guy Robinson Python 3 01-19-2005 11:01 PM
keyword argument for min/max Steven Bethard Python 1 11-30-2004 06:02 PM
a question on the new dict() keyword argument syntax Oktay Safak Python 2 08-02-2003 09:12 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57