Matplotlib and partial application of arguments

Hi list,

The program i have to create must let users give lots of paremeters for functions.
But he can only specify some and the program must adapt to complete by default settings the ones missing.
It is a way for python to compensate the lack of overload for methods in oo.

I go through pylab.py code and although it is new to me, i have the intuition you are using it.
Is Matplotlib using the concept of partial application?

Thans a lots,
regards,
Philippe Collet

philopensource@...32... wrote:

Hi list,

The program i have to create must let users give lots of paremeters for
functions.
But he can only specify some and the program must adapt to complete by
default settings the ones missing.
It is a way for python to compensate the lack of overload for methods in
oo.

I go through pylab.py code and although it is new to me, i have the
intuition you are using it.
Is Matplotlib using the concept of partial application?

Yes, pylab does this, although I've never heard default arguments called
a "concept of partial application." I might suggest that pylab's
handling of arguments is rather elaborate for a newbie with its heavy
use of *args and **kwargs and nested call structures, but don't let that
stop you! :slight_smile: In case you haven't seen it, check out the relevant part
of the tutorial: "More on Defining Functions"

http://docs.python.org/tut/node6.html#SECTION006700000000000000000

Cheers!
Andrew

philopensource@...32... wrote:

But he can only specify some and the program must adapt to complete by default settings the ones missing.
It is a way for python to compensate the lack of overload for methods in oo.

This is called keyword arguments in Python, and it has a lot of use besides "compensating for a lack of overloaded methods.

I go through pylab.py code and although it is new to me, i have the intuition you are using it.
Is Matplotlib using the concept of partial application?

Hardly any substantial Python code doesn't use keyword arguments.

By the way, you can do type checking as another way to get something like method overloading.

-Chris

···

--
Christopher Barker, Ph.D.
Oceanographer
                                         
NOAA/OR&R/HAZMAT (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception

Chris.Barker@...259...

The program i have to create must let users give lots of paremeters for functions.
But he can only specify some and the program must adapt to complete by default settings the ones missing.

It sounds like Python's keyword arguments are exactly what you need. They allow you to specify optional function arguments that are assigned some default value if no value is provided:

  >>> def foo(a=1, b=2, c=3):
  ... print a, b, c
  ...
  >>> foo()
  1 2 3
  >>> foo('x')
  x 2 3
  >>> foo('x', 'y')
  x y 3
  >>> foo('x', 'y', 'z')
  x y z

Python also allows you to specify the values of keyword arguments by name, rather than by position:

  >>> foo(a=47, c=12)
  47 2 12

Sometimes you want to have *a lot* of keyword arguments or you need to capture additional keyword arguments without caring what they are. Python allows you to do this with a parameter that starts with "**", e.g. "**kwds". When the function is called, this argument will be a dictionary containing the keyword arguments:

  >>> def bar(**kwds):
  ... print kwds
  ...
  >>> bar(a=1, b=2, c=3)
  {'a': 1, 'c': 3, 'b': 2}
  >>> bar(a='x', c='z')
  {'a': 'x', 'c': 'z'}

Python has some neat tricks to allow you to supply arguments to function calls using tuples and dictionaries, which is something matplotlib does a lot. It's really useful when you're just collecting arguments to pass to another function:

  import wx
  class TitleFrame(wx.Frame):
    def __init__(self, parent, title, **kwds):
      # pass the keyword arguments on to wx.Frame.__init__()
      wx.Frame.__init__(self, parent, -1, title, **kwds)

The link Andrew sent you provides a complete overview of all of these features, explains how the arguments to a function call get mapped onto its position and keyword arguments, and also covers all of the gotcha's in detail. For example, you'll learn why this happens and how to work around it:

  >>> def baz(a, b=[]):
  ... b.append(a)
  ... print b
  ...
  >>> baz(1)
  [1]
  >>> baz(2)
  [1, 2]
  >>> baz(3)
  [1, 2, 3]

It is a way for python to compensate the lack of overload for methods in oo.

I'm not sure how you plan to use default values to compensate for the absence of overloaded methods. The general view of the Python community seems to be that overloaded methods aren't really all that hot and doing type-based dispatching with isinstance() is genrally a Bad Thing. I've found that it isn't too difficult to design around the lack over overloading.

If you have some application that absolutely requires it, you may want to look into the Python Enterprise Application Kit, which has a multiple/predicate dispatch framework (much more powerful than simple overloading):

  http://peak.telecommunity.com/

I go through pylab.py code and although it is new to me, i have the intuition you are using it.

Oh yeah, matplotlib is chock full of keyword arguments. Most major Python libraries are, because they make programming so convenient. Pylab also makes heavy use of the **kws construct to pass keyword arguments around.

Is Matplotlib using the concept of partial application?

I think you probably meant to ask about "default arguments". Partial application is a functional programming concept where calling a function with fewer arguments than it requires returns another function, which accepts the remaining arguments. They're a PEP about adding this to Python 2.5:

  http://www.python.org/peps/pep-0309.html

Ken

···

On May 24, 2005, at 4:37 AM, philopensource@...32... wrote: