Passing a variable number of arguments to a wrapped function.

S

stephen

Is there a better way of doing this so that I don't have to go through
every permutation of possible arguments (the example here from the
matplotlib 'plot' function):

def makeplot(self, xvalues, yvalues, linecolor='', linewidth=''):
if linecolor and linewidth:
plot(xvalues, yvalues, linecolor, linewidth=linewidth)
elif linecolor:
plot(xvalues, yvalues, linecolor)
elif linewidth:
plot(xvalues, yvalues, linewidth=linewidth)
else:
plot(xvalues, yvalues)

Stephen
 
R

Robert Kern

Is there a better way of doing this so that I don't have to go through
every permutation of possible arguments (the example here from the
matplotlib 'plot' function):

Usually, you would just make the defaults for linecolor and linewidth
the same as the defaults for the underlying function. matplotlib.pylab
functions, however, have too much intelligence coded in them for this to
be easy.

--
Robert Kern
(e-mail address removed)

"In the fields of hell where the grass grows high
Are the graves of dreams allowed to die."
-- Richard Harter
 
G

Guest

On 5 Aug 2005 08:34:32 -0700
Is there a better way of doing this so that I don't have to go through
every permutation of possible arguments (the example here from the
matplotlib 'plot' function):

def makeplot(self, xvalues, yvalues, linecolor='', linewidth=''):
if linecolor and linewidth:
plot(xvalues, yvalues, linecolor, linewidth=linewidth)
elif linecolor:
plot(xvalues, yvalues, linecolor)
elif linewidth:
plot(xvalues, yvalues, linewidth=linewidth)
else:
plot(xvalues, yvalues)

What's wrong with:

def makeplot(self, xvalues, yvalues, **kwargs):
plot(xvalues, yvalues, **kwargs)

or even:

def makeplot(self, *a, **ka):
plot(*a, **ka)

?
 
J

John Hunter

stephen> Is there a better way of doing this so that I don't have
stephen> to go through every permutation of possible arguments
stephen> (the example here from the matplotlib 'plot' function):

You can make linecolor=None and linewidth=None, and then use
matplotlib's rc defaults

from matplotlib import rcParams
def makeplot(self, xvalues, yvalues, linecolor=None, linewidth=None):
if linecolor is None: linecolor = rcParams['lines.color']
if linewidth is None: linewidth = rcParams['lines.linewidth']
plot(xvalues, yvalues, color=linecolor, linewidth=linewidth)

Then you can customize the defaults in the rc file
(http://matplotlib.sf.net/matplotlibrc) any way you want.

Alternatively, you can also set the defaults in your script

from matplotlib import rc
rc('lines', linewidth=1.0, color='red')

JDH
 
B

Bengt Richter

Is there a better way of doing this so that I don't have to go through
every permutation of possible arguments (the example here from the
matplotlib 'plot' function):

def makeplot(self, xvalues, yvalues, linecolor='', linewidth=''):
if linecolor and linewidth:
plot(xvalues, yvalues, linecolor, linewidth=linewidth)
elif linecolor:
plot(xvalues, yvalues, linecolor)
elif linewidth:
plot(xvalues, yvalues, linewidth=linewidth)
else:
plot(xvalues, yvalues)

It seems like you are not changing the order or values of arguments,
so I'm wondering if this would work for you:

def makeplot(self, *args, **kwargs):
plot(*args, **kwargs)

and if not, why not.

Regards,
Bengt Richter
 
S

stephen

Using 'plot(*args, **kwargs)' does seem to work just fine.

Thanks to all for their suggestions.

Stephen
 

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

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top