Nested Parameter Definitions

Paddy paddy3118 at googlemail.com
Sun Feb 25 16:00:26 EST 2007


On Feb 25, 7:06 pm, "Virgil Dupras" <hardcoded.softw... at gmail.com>
wrote:
> On Feb 25, 1: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')
>
> > Ruben commented that there was a poll on this features continued
> > existence taken at PyCon and it could go.
>
> > Just as I found it, it could go
>
> > I wondered if those of you with some Python experience new of nested
> > parameters and don't use them; or just forgot/don't know it is
> > possible?
>
> > - Paddy.
>
> > Oh - the blog entry is athttp://paddy3118.blogspot.com/2007/02/pythons-function-nested-paramet...
>
> I didn't know about it either. Without the call example, I would have
> had a hard time to try to figure out what these extra brackets are
> for. For this reason, I think that an explicit unpack is more
> readable, and thus better.

The following example shows three possible ways of accessing nested
data. i think , after now knowing how to use it, that the f0 function
with nested parameters and its subsequent use is the most readable.
Manual unpacking in the list comprehension for f1 is messy. No real
reason for liking f0 over f2 except its shiny!

>>> def f0 ((p0, p1), p2):
... 	pass
...
>>> def f1 (p0, p1, p2):
... 	pass
...
>>> def f2(p):
... 	(p0, p1), p2 = p
...
>>> data = [ ((1, 2), 3),  ((4, 5), 6)]
>>> [ f0(*datum) for datum in data]
[None, None]
>>> [ f1(datum[0][0], datum[0][1], datum[1]) for datum in data]
[None, None]
>>> [ f2(datum) for datum in data]
[None, None]
>>>

- Paddy.




More information about the Python-list mailing list