Skip to content

Instantly share code, notes, and snippets.

@nicoddemus
Last active November 7, 2015 21:31
Show Gist options
  • Save nicoddemus/3d24094cfb553989712a to your computer and use it in GitHub Desktop.
Save nicoddemus/3d24094cfb553989712a to your computer and use it in GitHub Desktop.
Handling keyword arguments default values
# python 2
def foo(**kwargs):
x = kwargs.pop('x', 10)
y = kwargs.pop('y', 'no value')
if kwargs:
raise TypeError('unknown keyword arguments: %s' % kwargs.keys())
print x, y
# python 3
def foo(*, x=10, y='no value'):
print(x, y)
@hackebrot
Copy link

def foo(bar, hello='world'):
    pass

def test_foo_kwargs():
    foo(True)
    assert foo.kwargs == {'hello': 'world'}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment