473,396 Members | 1,847 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

How to call functions with list and keyword arguments?

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.

Jul 18 '05 #1
4 3945
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.

Jul 18 '05 #2
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" <an********************************@rambler.ru> wrote in
message news:bq**********@news.peterlink.ru...
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.

Jul 18 '05 #3
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.

Jul 18 '05 #4
John Z. Smith <jz*****@optonline.net> 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
rm***@pobox.com
Jul 18 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Chuck Anderson | last post by:
I have a function with 7 inputs. The last three have default values. I want to call that function specifying the first four, skip two and then specify the last. I thought I could write this...
0
by: Anthony Baxter | last post by:
To go along with the 2.4a3 release, here's an updated version of the decorator PEP. It describes the state of decorators as they are in 2.4a3. PEP: 318 Title: Decorators for Functions and...
16
by: David Bear | last post by:
I know there must be a better way to phrase this so google understands, but I don't know how.. So I'll ask people. Assume I have a list object called 'alist'. Is there an easy way to create a...
1
by: Khai Doan | last post by:
I have function A, which need to call function B with the exact same argument list. What is the correct way to do this? I had function A: function A { B(arguments); } but it does not...
9
by: CryptiqueGuy | last post by:
Consider the variadic function with the following prototype: int foo(int num,...); Here 'num' specifies the number of arguments, and assume that all the arguments that should be passed to this...
1
by: ptugarin | last post by:
Hi All, As part of my job, I need to wrap around an existing library of functions. Some functions have the prototype void foo(int keyword, ...) { } where ... could be a list of any size, of...
34
by: Srinu | last post by:
Hi all, Can we assign return value of a function to a global variable? As we know, main() will be the first function to be executed. but if the above is true, then we have a function call before...
12
by: Rahul | last post by:
Hi Everyone, I have the following code and i'm able to invoke the destructor explicitly but not the constructor. and i get a compile time error when i invoke the constructor, why is this so? ...
2
by: PatrickMinnesota | last post by:
I've been reading the docs and looking for an answer and seem stuck. I'm either not looking in the right places or not understanding what I'm reading. I have a bunch of functions. I want to put...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.