logoalt Hacker News

rileymat2today at 4:07 AM3 repliesview on HN

Python calls them keyword arguments.


Replies

tonyarklestoday at 5:14 AM

They do, but they're also not strictly required to be named explicitly:

They can be:

    >>> def foo(bar=None):
    ...   print(bar)
    ...
    >>> foo()
    None
    >>> foo(bar="baz")
    baz
    >>> foo(bar="baz", baz="azp")
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: foo() got an unexpected keyword argument 'baz'
But you can also use them generically:

    >>> def bar(**kwargs):
    ...   print(kwargs)
    ...
    >>> bar()
    {}
    >>> bar(site="HN", user="tonyarkles")
    {'site': 'HN', 'user': 'tonyarkles'}
cenamustoday at 4:32 AM

And I suppose Lisp started that with :key args

antonvstoday at 4:24 AM

Python’s perversity is fractal.