Nested Parameter Definitions

Hendrik van Rooyen mail at microcorp.co.za
Mon Feb 26 01:11:18 EST 2007


"Arnaud Delobelle" <arnodel at googlemail.com> wrote:


> On Feb 25, 6:00 pm, "Paddy" <paddy3... at googlemail.com> wrote:
> > I blogged on finding a new-to-me feature of Python, in that you are
> > allowed to nnest parameter definitions:
> >
> > >>> def x ((p0, p1), p2):
> >
> > ...     return p0,p1,p2
> > ...>>> x(('Does', 'this'), 'work')
> >
> > ('Does', 'this', 'work')
> 
> Reminds me of LeLisp! It had a similar feature.  IIRC you could write
> for example (I think 'df' was LeLisp for 'defun'):
> (df mycar (a . b) a)
> or
> (df mylist L L)
> or
> (df mycaadr (a (b . c) . e) b)
> 
> I didn't know that this was possible in python and it does surprise
> me.  It feels at odd with the python philosophy.
> 

Not at all - it much nicer than you think, and there is no "nesting" 
involved - Penguins carry their eggs on their feet.

The original function definition describes a function that has a two element
tuple as a first parameter, and something else as a second one.
The first two names provide a means of accessing the elements of the
tuple, instead of using slicing.

look at this:

>>> def f((a,b),c):
 return a,b,c

>>> tup = ('hi','there')
>>> f(tup,'foo')
('hi', 'there', 'foo')
>>> lis = ['hi','there']
>>> f(lis,'foo')
('hi', 'there', 'foo')
>>> d,e,f = f(lis,42)
>>> print d,e,f
hi there 42
>>> e
'there'
>>> s = 'go'
>>> f(s,'back')
('g', 'o', 'back')
>>>  

Long live duck typing...

- Hendrik





More information about the Python-list mailing list