Hacker News new | past | comments | ask | show | jobs | submit login

python also has this right by introducing named parameters. The same function in python can be written as:

  do_send_mail(recipient="some@email.com", cc="another@email.com", subject="Hello!", body="hi!")



There are significant differences though:

1. It's not possible to mandate the usage of keyword attributes in Python 2, this function could be called with

    do_send_mail("some@email.com", "another@email.com", "Hello!", "hi!")
JS/Ruby's low-fi version of using hashes actually helps there as you have to provide hash arguments via the hash.

Python 3 lets you fix this issue (a great reason to switch, by the way) by using a ⭑[0] argument (not a ⭑-arg, which you can use in Python 2 but which will simply make your stuff fall into a black hole unless the creator of the function asserts no positional argument was passed via e.g. `assert not arg`; furthermore Python 2 would require that a default value — to check for — be given for each keyword argument):

    >>> def foo(*, a, b):
    ...     print(a, b)
    ... 
    >>> foo()
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: foo() needs keyword-only argument a
    >>> foo(1, 2)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: foo() takes exactly 0 positional arguments     (2 given)
    >>> foo(a=1, b=2)
    1 2
2. the arguments are separate from the method name in Python, in your example the method is `do_send_mail`, in Smalltalk and Objective-C it's `doSendMailToRecipient:cc:subject:body:` which has advantages and inconvenients. Python is easier for default values (you need a new method in Smalltalk or Obj-C), but the Smalltalk/Obj-C method is simpler for behavioral differences (as the dispatch is done at callsite, Python will need internal soup)

3. There is no order in Python's argument (whereas changing the order in Smalltalk/Obj-C changes the method called), which means callers can lower the call's readability by swapping arguments around in... less than sensible ways.

(nb: I love Python, don't interpret my comment as a put down of it, just pointing out fundamental and important difference between Python's keyword arguments and Smalltalk's keyword messages)

[0] I hate HN's markup.


> It's not possible to mandate the usage of keyword attributes in Python 2

That's not completely true, but it's inconvenient. If you're prepared to go the kwargs route, calling with positional arguments raises a TypeError:

    >>> def foo(**kwargs):
    ...  print(kwargs['a'], kwargs['b'])
    ... 
    >>> foo(1, 2)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: foo() takes exactly 0 arguments (2 given)
    >>> foo(a=1, b=2)
    (1, 2)
You have to manually check for required parameters, though, and assign any necessary defaults.


> That's not completely true, but it's inconvenient. If you're prepared to go the kwargs route

Ah true, I'd not thought about using kwargs only.

> You have to manually check for required parameters, though, and assign any necessary defaults.

Yeah, and you likely lose parameters management in the IDE.


thanks for the point. I have no background in Python so I didn't take Python into consideration. In my opinion, there are more people like me: coming from pure HTML/CSS/PHP background straight to Objective C


Look into Smalltalk (which is the language Obj-C borrowed the message syntax from) too. Squeak by Example is an excellent intro.

http://squeakbyexample.org/




Consider applying for YC's W25 batch! Applications are open till Nov 12.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: