Python metaclasses
ivansmirnov.io
ivansmirnov.io
Another loosely related suggestion: if we could stay away from singleton as examples, I think we would save ourselves a lot of future troubles. ^^
I wouldn't quite say this - I wouldn't want to discourage someone who's going to learn them to go ahead, but I'd just say "Only use them when you really need to use them and if you're going to use them make sure you really understand them".
Can anyone maybe suggest a few examples of where this could be used (or is used already?)
We have a class that has different methods that performs numerical analysis/computations of financial models. These methods need to be tested with different subsets of data. The number of combinations of tests and test-data are huge, whereas the 'testing code' is simple and repetitive. Having these tests in a loop is not useful due to way test errors get reported.
Therefore, we generate a test class using the meta classes, that creates 'test_*' methods that are eventually executed. With such methods in place, we have around 100 tests generated within less than 100 lines of code, without loosing the detailed error reporting in case of failing tests.
In Django, when we add a new field type, you need to remember to test all the integrations (admin, forms, migrations, etc) and that's not always done correctly. It'd be great if there were some easier method than just subclassing a template test class and implementing all tests from the ground up.
With the subclass approach you are required to implement a new class and essentially write repetitive code, or reuse existing functions. Reusing existing test methods does not provide good error reporting as I had stated earlier. It is late in the day. Shortly, I will try to post a gist example of what I am talking about, if you are still interested.
http://ivansmirnov.io/python-metaclasses/#metaclass-as-a-reg...
This enables things like plugin systems, where you want the act of importing a plugin which defines some objects to load those objects into the available plugin list.
It's a very powerful language feature. Also very dangerous. It's a magnet for technical debt and obscure bugs.
https://github.com/bkeroack/elita/blob/27129ea64382275effa99...
https://github.com/bkeroack/elita/blob/27129ea64382275effa99...
There are many ways to enforce a constraint from dervied classes to base classes.
Metaclasses are the only* way to enforce a constraint from a base class to a derived class.
(* there's actually another way, but don't do it.)
But I do a fair amount of Python, and a 20 minute cruise through the Moose docs leads me to believe that the most similar thing to Moose in the Python world is, well, Python.
Effectively, they give you access to parametric polymorphism in perl land.
http://modernperlbooks.com/books/modern_perl_2014/07-object-...
Rather than having the kind of parametric polymorphism you get with roles, Python prefers duck typing. And for the practical use of adding some composed, reusable, but orthogonal state and behavior to a class, Python provides multiple inheritance. For type-checking issues where duck typing is not sufficient, Python has Abstract Base Classes.
Now, I am aware that roles are not the same thing as duck typing or multiple inheritance, or ABCs. Not having roles in Python is a design decision. http://legacy.python.org/dev/peps/pep-3133/
But I still think in terms of the ability to use the language to write clean OOP code, Python is closer to Perl5 + Moose, than Perl5 alone is to Perl5 + Moose.
(Not a knock on Perl5 or Moose, between 2002 and 2005, I did a bunch of web and systems programming in Perl, and I wish Moose existed then.)
TL;DR: Moose is an awesome OO framework for Perl.