<p><span class="h-card" translate="no"><a href="https://mastodon.social/@dabeaz" class="u-url mention">@<span>dabeaz</span></a></span> <span class="h-card" translate="no"><a href="https://rebel.ar/@nowis" class="u-url mention">@<span>nowis</span></a></span> <span class="h-card" translate="no"><a href="https://fosstodon.org/@brettcannon" class="u-url mention">@<span>brettcannon</span></a></span> </p><p>struct.Point(x=int, y=int)</p><p>It&#39;s almost the struct keyword, but kinda ugly underneath...</p><p>class Struct:<br /> def __getattr__(self, name):<br /> caller_globals = inspect.stack(0)[1].frame.f_globals</p><p> def make_dataclass(**args):<br /> new_class = dataclass(type(name, (), dict(__annotations__=args)))<br /> caller_globals[name] = new_class</p><p> return make_dataclass</p><p>struct = Struct()</p><p>struct.Point(x=int, y=int)</p><p>print(Point(x=1, y=2))</p>
Reply