How to call functions with list and keyword arguments?

J

John Z. Smith

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

anton muhin

John said:
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.
 
J

John Z. Smith

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

anton muhin

John said:
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.
 
R

Robin Munn

John Z. Smith said:
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
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top