multiple values for keyword argument

Discussion in 'Python' started by Tobias Blass, Jan 29, 2011.

  1. Tobias Blass

    Tobias Blass Guest

    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()
    Tobias Blass, Jan 29, 2011
    #1
    1. Advertising

  2. On 29 Gen, 12:10, Tobias Blass <> 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
    Francesco Bochicchio, Jan 29, 2011
    #2
    1. Advertising

  3. Tobias Blass

    Tobias Blass Guest

    On Sat, 29 Jan 2011, Francesco Bochicchio wrote:

    >On 29 Gen, 12:10, Tobias Blass <> 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?
    Tobias Blass, Jan 29, 2011
    #3
  4. 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.
    Frank Dierkes, Jan 29, 2011
    #4
  5. Tobias Blass

    Peter Otten Guest

    Tobias Blass wrote:

    >
    >
    > On Sat, 29 Jan 2011, Francesco Bochicchio wrote:
    >
    >>On 29 Gen, 12:10, Tobias Blass <> 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.
    Peter Otten, Jan 29, 2011
    #5
  6. Tobias Blass

    Roy Smith Guest

    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.
    Roy Smith, Jan 29, 2011
    #6
  7. Tobias Blass

    Ethan Furman Guest

    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~
    Ethan Furman, Jan 29, 2011
    #7
  8. Tobias Blass

    Tobias Blass Guest

    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 <> 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)
    Tobias Blass, Jan 29, 2011
    #8
  9. Tobias Blass

    Guest

    > 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
    >
    >
    , Jan 29, 2011
    #9
  10. 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/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:

    >>> 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
    Steven D'Aprano, Jan 29, 2011
    #10
  11. Tobias Blass

    Ethan Furman Guest

    wrote:
    >> 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. :)

    >
    > 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~
    Ethan Furman, Jan 30, 2011
    #11
  12. On Sat, 29 Jan 2011 10:39:49 -0800, patty wrote:

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




    --
    Steven
    Steven D'Aprano, Jan 30, 2011
    #12
  13. On 29 January 2011 18:39, <> wrote:
    >> 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.  :)

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

    --
    Cheers,
    Simon B.
    Simon Brunning, Jan 31, 2011
    #13
  14. Tobias Blass

    Guest

    > On 29 January 2011 18:39, <> wrote:
    >>> 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.  :)

    >>
    >> 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.
    >
    > --
    > Cheers,
    > Simon B.
    >
    >


    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
    , Jan 31, 2011
    #14
  15. 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
    Jean-Michel Pichavant, Jan 31, 2011
    #15
  16. Tobias Blass

    Patty Guest

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


    > 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
    Patty, Jan 31, 2011
    #16
  17. Patty wrote:
    >
    >> 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
    Jean-Michel Pichavant, Feb 1, 2011
    #17
  18. Tobias Blass

    Patty Guest

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


    > Patty wrote:
    >>
    >>> 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:

    >> "the assignment does not modify the original object, only changing which
    >> object that the rest of the code in the method refers to" <<


    "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
    Patty, Feb 1, 2011
    #18
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Oktay Safak
    Replies:
    2
    Views:
    1,316
    Oktay Safak
    Aug 2, 2003
  2. Steven Bethard

    keyword argument for min/max

    Steven Bethard, Nov 30, 2004, in forum: Python
    Replies:
    1
    Views:
    298
    Steven Bethard
    Nov 30, 2004
  3. Replies:
    6
    Views:
    441
    Peter Otten
    May 10, 2007
  4. Hamilton, William

    RE: keyword checker - keyword.kwlist

    Hamilton, William, May 10, 2007, in forum: Python
    Replies:
    4
    Views:
    343
  5. Replies:
    5
    Views:
    121
Loading...

Share This Page