Skip to content Skip to sidebar Skip to footer

Python Optional, Positional And Keyword Arguments

This is a class I have: class metadict(dict): def __init__(self, do_something=False, *args, **kwargs) if do_something: pass super(metadict,self).__i

Solution 1:

It's new in Python 3. The best workaround in Python 2 is

deffoo(*args, **kwargs):
    do_something = kwargs.pop("do_something", False)

The behaviour you see happens because Python tries to be clever in matching up arguments, so for instance it will make a keyword argument positional if you pass too many positional arguments.

PS why not store it as an attribute of metadict instead of as an entry in the dict?

Post a Comment for "Python Optional, Positional And Keyword Arguments"