Verbose and flexible args and kwargs syntax

Discussion in 'Python' started by Eelco Hoogendoorn, Dec 11, 2011.

  1. Throwing an idea for a PEP out there:

    It strikes me that the def func(*args, **kwargs) syntax is rather
    unpytonic. It certainly did not have that 'for line in file' pythonic
    obviousness for me as a beginner. Plus, asterikses are impossible to
    google for, so finding out what exactly they do more or less forces you
    to write a forum post about it.

    A more readable form occurred to me, which also happens to be more
    flexible, and which I think is fully compatible with the syntax of the
    language:

    def func(parg, list(args), dict(kwargs))

    Perhaps this is considered abuse of notation; dict(kwargs) already has a
    meaning rather different from the one we try to give it here; but then
    again the context (being inside a function definition) is unique and
    easily recognizable.

    An added advantage would be the possibility of using subclasses of dict
    and list as well; imagine how much more beautiful a lot of code would be
    if one could say

    def func(attrdict(kwargs))

    Problems im still wrestling with: the same syntax could not be used when
    calling a function; that lack of symmetry would make things more
    confusing, not less.

    Thoughts?
     
    Eelco Hoogendoorn, Dec 11, 2011
    #1
    1. Advertising

  2. On Sun, 11 Dec 2011 11:49:23 +0100, Eelco Hoogendoorn wrote:

    > Throwing an idea for a PEP out there:
    >
    > It strikes me that the def func(*args, **kwargs) syntax is rather
    > unpytonic. It certainly did not have that 'for line in file' pythonic
    > obviousness for me as a beginner. Plus, asterikses are impossible to
    > google for, so finding out what exactly they do more or less forces you
    > to write a forum post about it.


    No more so than any other form of punctuation. Plus and minus + - may be
    so common that just about everyone knows it, but how about | == @ % and
    even . (dot)? None of these things will be obvious to newbies who have
    never programmed before. Oh well.

    Some things you just have to learn.

    As for impossibility of googling for asterisks, it might help if you
    spell it correctly <wink>

    http://duckduckgo.com/?q=python asterisk

    Fourth hit says:

    *vargs puts all left-over non-keyword arguments into a
    tuple called vargs. **kargs puts all left-over keyword
    arguments into a dictionary called kargs.

    Even better, this works amazingly well:

    http://duckduckgo.com/?q=python *

    Or you can Read The Fine Manual, which has a section for special symbols:

    http://docs.python.org/genindex-Symbols.html

    which is moderately easy to discover from the main page: Main page =>
    Documentation => Browse Current Documentation => General Index =>
    Symbols. Not ideal, but not too bad.

    > A more readable form occurred to me, which also happens to be more
    > flexible, and which I think is fully compatible with the syntax of the
    > language:
    >
    > def func(parg, list(args), dict(kwargs))
    >
    > Perhaps this is considered abuse of notation; dict(kwargs) already has a
    > meaning rather different from the one we try to give it here; but then
    > again the context (being inside a function definition) is unique and
    > easily recognizable.


    I consider that excessively verbose as well as misleading.

    It is excessively verbose for the same reason as:

    let y equal x plus 1

    would be considered excessively verbose. I'm very fond of some languages
    with verbose syntax, like Hypertalk and OpenXION where you might say "add
    1 to x", but I don't want Python to become them.

    It's a judgement call as to where a language divides "cryptic punctuation
    line noise" and "useful short operators", and in my opinion * and ** tuple
    and dict unpacking fall strongly on the "useful short operators" side.
    Your opinion may differ, but luckily for me, the BDFL agrees with me :)

    It is also misleading because args are not collected into a list, but
    into a tuple. Worse, it suggests that one should be able to generalise to
    something like this:

    def func(parg, str(args), int(kwargs), my_func(more_args)):

    which is incoherent.

    And lastly, this would require either promoting list and dict to proper
    keywords, which will not happen, or worse, making them semi-keywords
    (treated as keywords in some contexts, but not in others), which also
    will not happen.

    > Problems im still wrestling with: the same syntax could not be used when
    > calling a function; that lack of symmetry would make things more
    > confusing, not less.


    Precisely.


    > Thoughts?


    People learn to spell strings "like this" instead of str(like this), and
    to use # for comments instead of REMARK or REM. It's not that much of a
    burden on them to learn to use * and **


    --
    Steven
     
    Steven D'Aprano, Dec 11, 2011
    #2
    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. Eelco Hoogendoorn

    Verbose and flexible args and kwargs syntax

    Eelco Hoogendoorn, Dec 11, 2011, in forum: Python
    Replies:
    0
    Views:
    163
    Eelco Hoogendoorn
    Dec 11, 2011
  2. Eelco Hoogendoorn

    Verbose and flexible args and kwargs syntax

    Eelco Hoogendoorn, Dec 11, 2011, in forum: Python
    Replies:
    0
    Views:
    191
    Eelco Hoogendoorn
    Dec 11, 2011
  3. Eelco Hoogendoorn

    Verbose and flexible args and kwargs syntax

    Eelco Hoogendoorn, Dec 11, 2011, in forum: Python
    Replies:
    88
    Views:
    1,065
    Grant Edwards
    Dec 17, 2011
  4. Eelco Hoogendoorn

    Verbose and flexible args and kwargs syntax

    Eelco Hoogendoorn, Dec 11, 2011, in forum: Python
    Replies:
    0
    Views:
    165
    Eelco Hoogendoorn
    Dec 11, 2011
  5. Eelco Hoogendoorn

    Verbose and flexible args and kwargs syntax

    Eelco Hoogendoorn, Dec 12, 2011, in forum: Python
    Replies:
    0
    Views:
    166
    Eelco Hoogendoorn
    Dec 12, 2011
Loading...

Share This Page