Sometimes, it amazes me how elegant Python is. A class system that is almost as powerful as CLOS, yet easy to use and comprehend. It is truly one of the most powerful programming systems in the world, if you show it a bit of love.

I have developed a lot of metaclasss code when designing PISI. It has made the XML processing (which is managed by the autoxml library I wrote) and CLI code exceptionally cool. I was making some cleanup for the next PISI release, and I happened upon a not so pleasant Constants class, which now looks great thanks to metaclasses:

Well, and what did I find particularly elegant? How easy it was to write the constantsingleton metaclass, and how I tested it, almost poetic.

Mind you, python 2.x is still problematic with metaclasses, I think you should be using python 3.x if you want to use them. I just had to deal with a meaningless error that vanished without any reason in 2.7.13, so please use these classes at your own risk! And do not forget to link to the PISI github and mind the license, which is GPL-3.0 for these files. I have written these useful tests for that annoying stability problem, though, so please do not forget to first test it in your unit test environment first.

Happy hacking!

Appreciation of Python’s Elegance: Metaclasses and Design Patterns

Leave a Reply

Your email address will not be published.

Translate »
%d bloggers like this: