Skip to content

Instantly share code, notes, and snippets.

@alexmacniven
Created November 18, 2019 16:21
Show Gist options
  • Save alexmacniven/409a40c412d689b7560346fef67b7ea0 to your computer and use it in GitHub Desktop.
Save alexmacniven/409a40c412d689b7560346fef67b7ea0 to your computer and use it in GitHub Desktop.
This gist captures the power of Python **kwargs.

Kwargs Power

This code snippet demonstrates how KwargsPower can take any number of **kwargs and it will store them as instance variables.

>>> class KwargsPower:
...   def __init__(self, *args, **kwargs):
...     for k, v in kwargs.items():
...       self.__setattr__(k, v)
      
>>> kp = KwargsPower(a=1, b=2, c=3)
>>> kp.__dict__
{'a': 1, 'b': 2, 'c': 3}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment