multiple values for keyword argument

T

Tobias Blass

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()
 
F

Francesco Bochicchio

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
 
T

Tobias Blass

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

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

Frank Dierkes

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.
 
P

Peter Otten

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:
.... s = "yadda"
.... def yadda(but_i_dont_want_to_call_it_self):
.... print but_i_dont_want_to_call_it_self.s
....yadda

You can provoke the same error with a function:
.... pass
....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.
 
R

Roy Smith

Frank Dierkes said:
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.
 
E

Ethan Furman

Roy said:
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~
 
T

Tobias Blass

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

... s = "yadda"
... def yadda(but_i_dont_want_to_call_it_self):
... print but_i_dont_want_to_call_it_self.s
...
yadda

You can provoke the same error with a function:

... pass
...
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)
 
P

patty

Roy said:
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~

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
 
S

Steven D'Aprano

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/577030-dualmethod-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:
.... def __init__(self, value='spam'):
.... self.attr = value
........ return obj.attr
....'spam'
 
E

Ethan Furman

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.

Careful about the names you make-up -- to aid yourself and others you
don't want to have dozen's of different names that all basically mean
'this instance that I'm currently working on'.

~Ethan~
 
S

Steven D'Aprano

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.


That's a shame, because `self` is actually very simple once you
understand the basic principles of object-oriented programming.

What names would you choose? Unless you're writing descriptors, or using
class methods, both of which should be considered advanced usage (highly
advanced for descriptors, moderately so for class methods), it's not like
every method needs a different descriptive first argument. In English,
"self", "this", "me" or "instance" would be good names. What else would
you use?


The idea of method syntax is that you start with an instance of some
class:

mystring = "hello world" # an instance of the str class

In procedural languages like C or Pascal, you would call a function and
give the string as an argument. Python supports this programming model,
and uses it for built-ins like len:

len(mystring)
=> returns 11


Object oriented programming uses a different syntax. Instead of

function(instance)

as above, we take the instance argument outside the brackets. For example:

mystring.upper() # instead of upper(mystring)
=> returns "HELLO WORLD"

If there are any extra arguments needed, they go inside the brackets as
normal.

So far, this is just a change of syntax. It's like saying "The cat of my
brother's" vs. "my brother's cat" -- the meaning is the same, but the
syntax differs. The real advantages of object oriented programming and
methods come elsewhere (e.g. encapsulation and inheritance).

[Aside: when I was learning this, the hardest part I found was
remembering which things were functions, and which were methods.
I kept writing (wrongly!) things like:

"hello world".len()
upper("hello world")

Unfortunately there is no easy way to recognise what will be a
function like len, and which are methods like upper. That will
come with experience.

Back to function/procedural syntax versus object oriented syntax...

One difference, though, is when you write a method definition. Because
the method you write starts off life as an ordinary function, you need to
write it *as if* it were a function you call like len() above. Here's how
you might write a method in a class:

class MyClass:
def method(self, extra):
pass

When you then call the method:

instance = MyClass()
instance.method("something extra")

Python automatically changes the syntax for you, and passes two arguments
to the function as if you did this:

# pseudo-code
set self = instance
set extra = "something extra
extract "method" from MyClass
call method(self, extra)

We call the first argument something like "self" because it will ALWAYS
be the instance itself. Unlike a regular function, which can have
anything passed as the first argument, and therefore you should give it a
descriptive name, the method's first argument is provided automatically
for you and will always be the instance you started with.



I hope this helps.
 
S

Simon Brunning

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.

It's perfectly legal to use a name other than self. It's alo perfectly
legal never to wash - and you won't make any friends that way either.
 
P

patty

It's perfectly legal to use a name other than self. It's alo perfectly
legal never to wash - and you won't make any friends that way either.

Cute.

Believe me, I am learning to change my evil ways. This is what happens
when you come from a *long* line of self-employed, entrepreneurial people.

Independently Yours,

Patty
 
J

Jean-Michel Pichavant

I have been avoiding understanding this 'self',
[snip]
Regards,

Patty
What is to be understood ?? self references the instance. Did I miss
something ?

JM
 
P

Patty

----- Original Message -----
From: "Jean-Michel Pichavant" <[email protected]>
To: <[email protected]>
Cc: <[email protected]>
Sent: Monday, January 31, 2011 11:35 AM
Subject: Re: multiple values for keyword argument

I have been avoiding understanding this 'self',
[snip]
Regards,

Patty
What is to be understood ?? self references the instance. Did I miss
something ?

JM

Yes, there was more. And it's been fully explained at this point.

Patty
 
J

Jean-Michel Pichavant

Patty said:
I have been avoiding understanding this 'self',
[snip]
Regards,

Patty
What is to be understood ?? self references the instance. Did I miss
something ?

JM

Yes, there was more. And it's been fully explained at this point.

Patty


Hmm... I re-read the thread just in case ... Anyway.
I'd like to read suggestions for self replacements...
Currently 'yo' have been proposed.

I'm just curious, I promise I won't make any comment :D

JM
 
P

Patty

----- Original Message -----
From: "Jean-Michel Pichavant" <[email protected]>
To: "Patty" <[email protected]>
Cc: <[email protected]>
Sent: Tuesday, February 01, 2011 2:27 AM
Subject: Re: multiple values for keyword argument

Patty said:
(e-mail address removed) wrote:
I have been avoiding understanding this 'self',
[snip]
Regards,

Patty

What is to be understood ?? self references the instance. Did I miss
something ?

JM

Yes, there was more. And it's been fully explained at this point.

Patty


Hmm... I re-read the thread just in case ... Anyway.
I'd like to read suggestions for self replacements...
Currently 'yo' have been proposed.

I'm just curious, I promise I won't make any comment :D

JM

Hi Jean-Michel -

I'm sorry. I was getting sensitive about being criticized (or trying to
re-explain what I learned and getting it terribly mangled). As it turns
out - Westley Martinez pointed out the link about the usage of 'self' :
http://en.wikipedia.org/wiki/Self_(computer_science) and these specific
two lines showed me why I had been thinking I (we) could replace the word
'self' with any descriptive word (variable) I want. I was thinking of
'self' as a variable and not "In Python, there is no keyword for this, but
it exists as the name of the obligatory first argument of all member
functions. Conventionally, the name self is used."

And since I come from a C background, I thought I could do the following
(which the wiki page mentions) :} ) , thus I wanted to use an invented
variable name that makes sense to me and helped me as programmer remember
where I was in my program when I went and tried to reassign new values,
basically override my object at will. But this is what I did not realize:

"Some languages, such as Objective-C, allow assignment to self, although it
is deprecated."

And then after the thread ended - I read this in the wiki page which totally
explains everything -- "Early versions of C++ would let the this pointer be
changed; by doing so a programmer could change which object a method was
working on" and I learned C++ from an early version so this is Precisely
what I thought I could do -- I was envisioning having my object (member
function) and then as my program forked different paths, well I thought I
could do this very program design. Hopefully that makes more sense as to
why I would change the 'name of the obligatory first argument of all member
functions'.

As other people pointed out, you _can_ make up your own name, 'yo' or
anything else, it is by _convention_ to use 'self' and by doing your own
thing, could develop a bad habit.

Regards,

Patty
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,169
Latest member
ArturoOlne
Top