Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Named arguments is syntatic sugar for packing/unpacking data structures in some languages, so you can leverage that. In Python:

    >>> from collections import namedtuple
    >>> Vector2 = namedtuple('Vector2', ('x', 'y'))
    >>> PlayerOptions = namedtuple('PlayerOptions', ('position', 'size'))
    >>> opt = PlayerOptions(position=Vector2(100, 100), size=Vector2(50, 50))
    >>> Player(**opt._asdict())
The order arguments are passed doesn't matter anymore. You can also enforce this calling convention with a constructor signature like this (in 3):

    >>> class Player(object):
    >>>     def __init__(self, *, size, position):


i agree that in languages without compiler defined types, but with other tools such as named arguments, the class of errors described here can be to some extent avoided by making good use of named arguments. I'm gonna do this more often in my python code!




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

Search: