Post AVwXRtsMjWT6RLlzu4 by jamey@toot.cat
(DIR) More posts by jamey@toot.cat
(DIR) Post #AVwXRtsMjWT6RLlzu4 by jamey@toot.cat
2023-05-23T08:09:15Z
1 likes, 0 repeats
Python modules are objects. I didn't entirely expect that but it's reasonably consistent with the rest of Python's design, so I don't mind.Also, Python objects allow overriding a method named __getattr__ to run whatever code you want if somebody tries to look up an attribute that doesn't exist.Since Python modules are objects, modules can define __getattr__ and make names magically appear… Here's a definition which always returns None but first prints the name you tried to find:def __getattr__(name): print(name)You can put that definition in a file named say.py, then import say. At that point you can use expressions like, say, say.hello to print "hello".