4 ms·
If the name is mandatory, as it is in Smalltalk, then this basically solves the issue. If the name is optional, as it usually is in eg. Python, then there is a
by rntz 7y ago
If the name is mandatory, as it is in Smalltalk, then this basically solves the issue.
If the name is optional, as it usually is in eg. Python, then there is a temptation for the writer to omit it, which means the reader won't know what "True" or "False" stands for.
Are there languages besides Smalltalk and its descendants with mandatorily-named parameters?
- rand_r 7y agoIn Python 2, if you need to, you can force people to use kwargs by defining the function in the form of def f(*args, **kwargs): and then making sure any params you use in the function come from kwargs, and raising an error if args is non-empty. Python 3 has a nicer way of doing the same thing, that I can’t remember. (Sourced from the book “Effective Python: 59 Specific Ways to Write Better Python”.)
- dbcurtis 7y agoPlease tell me you have not been coding in Python for the last five years.
- rand_r 7y agoHuh? Why?
- Cpoll 7y agoIn Python 3 at least, anything after the * args is a required keyword-only argument: >>> def a(b, *args, c): print(c) >>> a() Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: a() missing 1 required positional argument: 'b' >>> a(3) Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: a() missing 1 required keyword-only argument: 'c' >>> a(3, c=5) 5 It's also valid to define it this way, if you don't need the * args: >>> def a(b, *, c): print(c)