How to call functions with list and keyword arguments?

Discussion in 'Python' started by John Z. Smith, Nov 26, 2003.

  1. Hi,
    I want to subclass a class (more precisely, optparse.Option). that looks
    like

    class Option:
    def __init__(self, *opts, **attrs):
    do_something()


    I want to do something in my __init__ and and also call Option.__init__.
    Then I don't know how to pass the arguments. For example,

    class MyOption:
    def __init__(self, *opts, **attrs):
    do_my_own_thing()
    Option.__init__(self, opts, attrs)

    does not work (and you know why). I figured out some very ugly ways to do
    this
    but I believe there should be an elegant way to pass the argments to
    superclass.
     
    John Z. Smith, Nov 26, 2003
    #1
    1. Advertising

  2. John Z. Smith

    anton muhin Guest

    John Z. Smith wrote:
    > Hi,
    > I want to subclass a class (more precisely, optparse.Option). that looks
    > like
    >
    > class Option:
    > def __init__(self, *opts, **attrs):
    > do_something()
    >
    >
    > I want to do something in my __init__ and and also call Option.__init__.
    > Then I don't know how to pass the arguments. For example,
    >
    > class MyOption:
    > def __init__(self, *opts, **attrs):
    > do_my_own_thing()
    > Option.__init__(self, opts, attrs)
    >
    > does not work (and you know why). I figured out some very ugly ways to do
    > this
    > but I believe there should be an elegant way to pass the argments to
    > superclass.
    >
    >
    >


    Option.__init__(self, *opts, **attrs) should work. BTW, some purists
    recommend to use super ;)

    hth,
    anton.
     
    anton muhin, Nov 26, 2003
    #2
    1. Advertising

  3. Thanks. I didn't event know that's legal syntax. Just checked the tutorial
    and reference
    manual but didn't find it. Could you point me to it? Thanks again.

    "anton muhin" <> wrote in
    message news:bq2iog$ak8$...
    > John Z. Smith wrote:
    > > Hi,
    > > I want to subclass a class (more precisely, optparse.Option). that

    looks
    > > like
    > >
    > > class Option:
    > > def __init__(self, *opts, **attrs):
    > > do_something()
    > >
    > >
    > > I want to do something in my __init__ and and also call Option.__init__.
    > > Then I don't know how to pass the arguments. For example,
    > >
    > > class MyOption:
    > > def __init__(self, *opts, **attrs):
    > > do_my_own_thing()
    > > Option.__init__(self, opts, attrs)
    > >
    > > does not work (and you know why). I figured out some very ugly ways to

    do
    > > this
    > > but I believe there should be an elegant way to pass the argments to
    > > superclass.
    > >
    > >
    > >

    >
    > Option.__init__(self, *opts, **attrs) should work. BTW, some purists
    > recommend to use super ;)
    >
    > hth,
    > anton.
    >
     
    John Z. Smith, Nov 26, 2003
    #3
  4. John Z. Smith

    anton muhin Guest

    John Z. Smith wrote:

    > Thanks. I didn't event know that's legal syntax. Just checked the tutorial
    > and reference
    > manual but didn't find it. Could you point me to it? Thanks again.
    >


    You're welcome.

    In my ActiveState distributive it's under Python Documentation\Language
    Reference\Expressions\Primaries\Calls. I hope ActiveState follows
    standard reference.

    regards,
    anton.
     
    anton muhin, Nov 26, 2003
    #4
  5. John Z. Smith

    Robin Munn Guest

    John Z. Smith <> wrote:
    > Thanks. I didn't event know that's legal syntax. Just checked the tutorial
    > and reference
    > manual but didn't find it. Could you point me to it? Thanks again.


    Looking through the manual myself, it's a little hard to find. The tutorial
    mentions the *args syntax in section 4.7.4:

    http://www.python.org/doc/current/tut/node6.html#SECTION006740000000000000000

    but never mentions the **kwargs syntax at all that I could find. I believe
    the tutorial could stand to be updated in that respect, maybe by expanding
    section 4.7.4. If the *args syntax is in there, **kwargs should be also.

    The language reference has mentions the syntax in the section on callable
    objects, 5.3.4:

    http://www.python.org/doc/current/ref/calls.html

    And finally, the extended call syntax is mentioned in the description of the
    builtin function apply(), as the reason why apply() is now deprecated:

    http://www.python.org/doc/current/lib/built-in-funcs.html

    --
    Robin Munn
     
    Robin Munn, Nov 29, 2003
    #5
    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. Edward Diener
    Replies:
    14
    Views:
    5,117
    Josiah Carlson
    Apr 6, 2004
  2. Fuzzyman
    Replies:
    2
    Views:
    312
    Fuzzyman
    Feb 22, 2005
  3. Armando Serrano Lombillo

    Built-in functions and keyword arguments

    Armando Serrano Lombillo, Oct 29, 2007, in forum: Python
    Replies:
    15
    Views:
    1,011
    Gabriel Genellina
    Oct 31, 2007
  4. Peter Motzfeldt
    Replies:
    1
    Views:
    173
  5. oldyork90
    Replies:
    10
    Views:
    368
    Jorge
    Sep 27, 2008
Loading...

Share This Page