Nested Parameter Definitions

Discussion in 'Python' started by Paddy, Feb 25, 2007.

  1. Paddy

    Paddy Guest

    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 at http://paddy3118.blogspot.com/2007/02/pythons-function-nested-parameter.html
    Paddy, Feb 25, 2007
    #1
    1. Advertising

  2. On Feb 25, 1:00 pm, "Paddy" <> 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.
    Virgil Dupras, Feb 25, 2007
    #2
    1. Advertising

  3. On Feb 25, 6:00 pm, "Paddy" <> 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.

    --
    Arnaud
    Arnaud Delobelle, Feb 25, 2007
    #3
  4. Paddy

    Paddy Guest

    On Feb 25, 7:06 pm, "Virgil Dupras" <>
    wrote:
    > On Feb 25, 1:00 pm, "Paddy" <> 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.
    Paddy, Feb 25, 2007
    #4
  5. On 25 Feb 2007 10:00:31 -0800, "Paddy" <>
    declaimed the following in comp.lang.python:

    > 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')
    > >>>

    >


    Just looks like an extension of the normal tuple unpacking feature
    of the language.

    a = ("Does", "This")
    x(a, "work")
    --
    Wulfraed Dennis Lee Bieber KD6MOG

    HTTP://wlfraed.home.netcom.com/
    (Bestiaria Support Staff: )
    HTTP://www.bestiaria.com/
    Dennis Lee Bieber, Feb 25, 2007
    #5
  6. On Sun, 25 Feb 2007 10:00:31 -0800, Paddy wrote:

    > 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?


    I learnt about this some time ago. I don't often use it, although it makes
    sense to write this:

    def parrot(x, (y, z)):
    pass

    instead of this:

    def parrot(x, a2tuple):
    y, z = a2tuple
    pass



    --
    Steven D'Aprano
    Steven D'Aprano, Feb 26, 2007
    #6
  7. On Sun, 25 Feb 2007 11:06:03 -0800, Virgil Dupras wrote:

    > On Feb 25, 1:00 pm, "Paddy" <> 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


    [snip]

    > 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.


    And now that you do know, it is not hard to figure it out at all.

    Nested parameters are no harder to figure out than *args or arg=value.
    It's something that you learn, once, and then it is easy forever.

    To my mind, def x ((p0, p1), p2) _is_ an explicit unpack. It just takes
    place in the parameter definition, where you can see it even if the
    source code is not available, not in the body of the function code, where
    you may or may not even be able to find it.



    --
    Steven D'Aprano
    Steven D'Aprano, Feb 26, 2007
    #7
  8. En Sun, 25 Feb 2007 15:00:31 -0300, Paddy <>
    escribió:

    >>>> def x ((p0, p1), p2):

    > ... return p0,p1,p2


    The first time I saw it used was in Zope, a long time ago. And I like it.
    Of course it only has any sense if you expect the tuple (p0,p1) to exist
    *before* the function is called; by example, when it's the return value of
    some other function.

    --
    Gabriel Genellina
    Gabriel Genellina, Feb 26, 2007
    #8
  9. "Arnaud Delobelle" <> wrote:


    > On Feb 25, 6:00 pm, "Paddy" <> 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
    Hendrik van Rooyen, Feb 26, 2007
    #9
  10. Paddy

    Guest

    Virgil Dupras:

    > 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.


    I can't agree.

    Bye,
    bearophile
    , Feb 26, 2007
    #10
  11. Paddy

    Paddy Guest

    On Feb 25, 11:41 pm, Dennis Lee Bieber <> wrote:

    > Just looks like an extension of the normal tuple unpacking feature
    > of the language.
    >


    Yep, it looks like good Python to me too. Maybe the tutorial could be
    extended to cover this form of parameter too?
    It would be good if Brett Cannon could add some comments on why he
    asked for a show of hands on the features use?
    I'd hate for the feature to be removed because it is not often used,
    when the reason it is not often used is because it is hard to find
    examples of its use, because its buried in the language reference
    manual.

    Do any Python books mention nested parameters?

    - Paddy.
    Paddy, Feb 26, 2007
    #11
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Tony Johansson

    Nested class definitions

    Tony Johansson, May 19, 2005, in forum: C++
    Replies:
    1
    Views:
    427
    Victor Bazarov
    May 19, 2005
  2. Fred Ma
    Replies:
    8
    Views:
    340
    Fred Ma
    Apr 13, 2004
  3. Jonah Bloch-Johnson

    Nested Arrays in Method Definitions--a puzzle

    Jonah Bloch-Johnson, Aug 20, 2008, in forum: Ruby
    Replies:
    3
    Views:
    110
    Jonah Bloch-Johnson
    Aug 20, 2008
  4. Jon
    Replies:
    0
    Views:
    268
  5. fl
    Replies:
    5
    Views:
    260
    Jorgen Grahn
    Nov 24, 2012
Loading...

Share This Page