• +43 660 1453541
  • contact@germaniumhq.com

Force Named Argument Calling in Python Revisited


Force Named Argument Calling in Python Revisited

If you remember the previous article on how to force function calling, by always specifying the argument names in our previous article, we were going with an args argument that captured extra junk. Turns out there’s an easier way. Simply use . Unbelievable!

Simply implementing this:

def update(*, major_version, minor_version):
    # original code
    pass

instead of the suggested implementation in the previous article:

def update(*args, major_version, minor_version):  # WRONG APPROACH
    if args:
        raise Exception("You need to use argument names")

    # original code

It’s enough.

If you try to call it with arguments, you’ll get a TypeError directly from python stating that the function doesn’t receive positional arguments.

Thanks to Christopher Prengel for pointing this one out.