Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > How to call functions with list and keyword arguments?

Reply
Thread Tools

How to call functions with list and keyword arguments?

 
 
John Z. Smith
Guest
Posts: n/a
 
      11-26-2003
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.



 
Reply With Quote
 
 
 
 
anton muhin
Guest
Posts: n/a
 
      11-26-2003
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.

 
Reply With Quote
 
 
 
 
John Z. Smith
Guest
Posts: n/a
 
      11-26-2003
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" <(E-Mail Removed)> wrote in
message news:bq2iog$ak8$(E-Mail Removed)...
> 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.
>



 
Reply With Quote
 
anton muhin
Guest
Posts: n/a
 
      11-26-2003
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.

 
Reply With Quote
 
Robin Munn
Guest
Posts: n/a
 
      11-29-2003
John Z. Smith <(E-Mail Removed)> 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/tu...00000000000000

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/li...-in-funcs.html

--
Robin Munn
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
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: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
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
functions and named keyword arguments Fuzzyman Python 2 02-22-2005 08:00 AM
please help me in distinguish redefining functions, overloading functions and overriding functions. Xiangliang Meng C++ 1 06-21-2004 03:11 AM



Advertisments