Class as Syntactic Sugar of Functions in Python
More explanation coming up, but essentially I’ve tried to define classes with functions in Python for a couple times already, but this time I have something that looks pretty good.
To use it, you just have to provide the class name and instance variables and methods:
make_class(
<class_name>,
{'__init__': <...>, 'instance_variable_or_method': <...>})
Essentially I used decorators to allow users to define functions that take in self
as the first argument but call those functions without self
. Also note, the init doesn’t have to return anything just like regular Python. The client code starts at line 22
.