News for hackers. Stuff that matters.
Re:Problems once solved by a metaclass can be solved by __in... by Sohcahtoa82 on Wednesday January 05, 2022 @04:08PM (#29814961)
I recently used them to create a framework for handling plugins to an IRC bot.

By creating an IrcMessageHandler metaclass, I can easily subclass it to encapsulate functionality into different classes without having to add boilerplate to load each plugin. There's a Plugins directory with a __init__.py that automatically loads all .py files, and the main bot just has to do `from Plugins import *` to load them all. Each plugin script merely needs a `from ircbot import IrcMessageHandler`, then creates a subclass. So there's a Calc class that adds a "!calc" command, Seen class that adds "!seen", and more. Each of them merely needs to include an "on_message" function that parses IRC messages.

There are no comments on this comment yet.