Skip to content

Instantly share code, notes, and snippets.

@jacksmith15
Last active October 2, 2019 10:25
Show Gist options
  • Save jacksmith15/515df6f060f7c551d1e77b1d0bd7bf88 to your computer and use it in GitHub Desktop.
Save jacksmith15/515df6f060f7c551d1e77b1d0bd7bf88 to your computer and use it in GitHub Desktop.
from collections import namedtuple
class ImmutableNamespaceMeta(type):
def __new__(cls, name, bases, dct):
x = super().__new__(cls, name, bases, dct)
public = {key: value for key, value in dct.items() if not key.startswith("_")}
typ_ = namedtuple(name + "Type", public.keys())
return typ_(**public)
class ImmutableNamespace(metaclass=ImmutableNamespaceMeta):
FOO = 3
BAR = "baz"
# TODO(Inheritance doesn't work)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment