Reputation: 8068
I'm trying to mix default keyword arguments, positional arguments and keyword arguments in Python 2.7. From the following code, I expect profile=='system'
, args==(1,2,3)
and kwargs={testmode: True}
.
def bla(profile='system', *args, **kwargs):
print 'profile', profile
print 'args', args
print 'kwargs', kwargs
bla(1, 2, 3, testmode=True)
What I get is:
profile 1
args (2, 3)
kwargs {'testmode': True}
Can this be done in Python 2.7 or do I need Python 3.x?
Upvotes: 0
Views: 1955
Reputation: 879361
In Python2:
def bla(*args, **kwargs):
profile = kwargs.pop('profile', 'system')
print 'profile', profile
print 'args', args
print 'kwargs', kwargs
In Python3 it's possible to define keyword-only arguments:
def bla(*args, profile='system', **kwargs):
print('profile', profile)
print('args', args)
print('kwargs', kwargs)
the call bla(1, 2, 3, testmode=True)
yields
profile system
args (1, 2, 3)
kwargs {'testmode': True}
Upvotes: 2