Thomas 'PointedEars' Lahn wrote: > In theory, it should be possible to substitute “math” with a reference to > an object that acts as a proxy for the original “math” module object but > whose base class declares the attributes for all the constants read-only.
#!/usr/bin/env python3 #--------------------------------------------------------------------------- import importlib class MyMath(object): def getter_factory (prop): def getter (self): return getattr(importlib.__import__("math", globals(), locals(), [prop], 0), prop) return getter pi = property(getter_factory("pi"), lambda self, val: None, lambda self: None) e = property(getter_factory("e"), lambda self, val: None, lambda self: None) inf = property(getter_factory("inf"), lambda self, val: None, lambda self: None) nan = property(getter_factory("nan"), lambda self, val: None, lambda self: None) def __getattr__ (self, name): return getattr(importlib.__import__("math", globals(), locals(), [name], 0), name, None) math = MyMath() print(math.pi, math.e, math.inf, math.nan) math.pi = 42 math.e = 42 math.inf = 42 math.nan = 42 print(math.pi, math.e, math.inf, math.nan) print(math.ceil(2.41)) #--------------------------------------------------------------------------- Using property() instead of decorators here was intended to achieve DRY by using a loop, but I could not find a way to define class attributes dynamically. Suggestions? -- PointedEars Twitter: @PointedEars2 Please do not cc me. / Bitte keine Kopien per E-Mail. -- https://mail.python.org/mailman/listinfo/python-list