5

Both C# and Python allow named arguments, so you can write something like: foo(bar:1). This is great, especially in combination with optional arguments.

My question is: what are the differences between the C# and Python named arguments, if any? I'm not interested in which is the "best", but in whether there are differences and in the possible motivations behind these differences.

And if someone knows of differences with other languages' implementations of this feature (Ruby or Objective-C, maybe), that could be interesting too.

edited to make community-wiki

1
  • To what end is this question? I think that if there isn't a sufficient background, this question could get subjective... Commented Feb 2, 2010 at 21:48

2 Answers 2

6

Python lets you "catch" unspecified named arguments into a dict, which is pretty handy

>>> def f(**kw):
...     print kw
... 
>>> f(size=3, sides=6, name="hexagon")
{'sides': 6, 'name': 'hexagon', 'size': 3}
Sign up to request clarification or add additional context in comments.

Comments

5

Python not only lets you catch unspecified named arguments into a dict, but also lets you unpack a dict into arguments:

    >>> def f(alfa, beta, gamma):
    ...     print alfa, beta, gamma
    ...
    >>> f(**{'alfa': 1, 'beta': 2, 'gamma': 3})
    1 2 3

and pass them down the stream:

    >>> def g(**kwargs):
    ...     f(**kwargs)
    ...
    >>> g(**{'alfa': 1, 'beta': 2, 'gamma': 3})
    1 2 3

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.