|
From: Ken M. <mc...@ii...> - 2005-05-24 17:06:15
|
On May 24, 2005, at 4:37 AM, phi...@ho... wrote:
> 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
|