Skip to content

Instantly share code, notes, and snippets.

@an-dyy
Last active May 17, 2022 20:51
Show Gist options
  • Save an-dyy/542170db935d86e6279f7018ef3a5259 to your computer and use it in GitHub Desktop.
Save an-dyy/542170db935d86e6279f7018ef3a5259 to your computer and use it in GitHub Desktop.
from __future__ import annotations
class DictHack:
__eq__ = lambda self, other: [other.__setitem__(key, value) for key, value in self.payload.items()]
def __init__(self, target: type["Any"], **kwargs: "Any") -> None:
self.target = target.__dict__
self.payload = kwargs
def __enter__(self) -> DictHack:
self.target == self
return self
def __exit__(self, *_: "Any") -> None:
...
with DictHack(str, hello=lambda s: print(f"Hello, {s}")):
"World!".hello()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment