4 ms·
This is an interesting and fairly well explained post, but a little light on real world examples. Can anyone maybe suggest a few examples of where this could b
by mrsuprawsm 11y ago
This is an interesting and fairly well explained post, but a little light on real world examples.
Can anyone maybe suggest a few examples of where this could be used (or is used already?)
- pandler 11y agoThe Django ORM or sqlalchemy good starting points. Metaclasses are what make them tick.
- akerl_ 11y agoThe registry really does seem to be the most common use case: http://ivansmirnov.io/python-metaclasses/#metaclass-as-a-registry 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.
- bkeroack 11y agoI've used metaclasses to implement debug logging, where every class method logs its parameters and return values: https://github.com/bkeroack/elita/blob/27129ea64382275effa99d727f3dd7d9d2e1b47b/elita/dataservice/models.py#L33 https://github.com/bkeroack/elita/blob/27129ea64382275effa99... https://github.com/bkeroack/elita/blob/27129ea64382275effa99d727f3dd7d9d2e1b47b/elita/util/__init__.py#L20-L25 https://github.com/bkeroack/elita/blob/27129ea64382275effa99...
- crdoconnor 11y agoI've written a few code spikes using metaclasses and to be honest, it never worked out well for me. I always found a simpler, clearer way of achieving the same outcome without them and the code that I did write felt dirty. It's a very powerful language feature. Also very dangerous. It's a magnet for technical debt and obscure bugs.
- stuaxo 11y agoI've had fun using them to make quasi-orms for CSV and Excel spreadsheet data before.
- Retra 11y agoI've used it to simulate new language features. It sort of allows you to program the type system.
- aldanor 11y agoHere's one good example: abc (abstract base classes) from Python's standard library -- https://hg.python.org/cpython/file/3.4/Lib/abc.py https://hg.python.org/cpython/file/3.4/Lib/abc.py
- cnvogel 11y agoI've used them to build a serialization/deserialization library for a proprietary protocol. To cut down on needless boilerplate I had the metaclass build setter/getter/en- and decoders from a static member variable. Inspired also by how Django describes its data model.
- grdvnl 11y agoWe use meta-classes extensively. One of the simpler examples I have used are as follows: 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.
- jsmeaton 11y agoCould the same thing be achieved by subclassing your base test class and then overriding setUp to inject the correct data for that combination? I'm pretty sure I'm oversimplifying, but I'm really interested in your method. 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.
- grdvnl 11y agoI do not think it would work with sub-classing. With the approach we have it is just easier to add the 'new' method name to test into the list argument of the meta class. The meta class would generate a `test` method with the passed in function name. 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.
- jsmeaton 11y agoBit late to respond, but yes I would be interested, thanks!