Mastodon Feed: Post

Mastodon Feed

Boosted by glyph ("Glyph"):
treyhunner ("Trey Hunner 🐍") wrote:

Python Tip #276 (of 365):

Make self positional-only when accepting "**kwargs".

This class accepts arbitrary keyword arguments... except for self:

>>> class Namespace:
... def __init__(self, **kwargs):
... for name, value in kwargs.items():
... setattr(self, name, value)
...
>>> space = Namespace(other=2, self=1)
Traceback (most recent call last):
...
TypeError: Namespace.__init__() got multiple values for argument 'self'

🧵 (1/3)

#Python #DailyPythonTip