Live data from Hacker News

Python metaclasses

ivansmirnov.io

1–10 of 30 posts

Re: Python metaclasses

#2
Great tool, but not to be put in the wrong hands. The folks at Django got it right. We used it for unifying, simplifying and ensuring consistency of deeply nested configuration data entered using different means. However, in environment with less experience, I have seen "Akira-final-scene-tetsuo" monsters come to life with this.

Another loosely related suggestion: if we could stay away from singleton as examples, I think we would save ourselves a lot of future troubles. ^^

Re: Python metaclasses

#3
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?)

Re: Python metaclasses

#4
post #2

Great tool, but not to be put in the wrong hands. The folks at Django got it right. We used it for unifying, simplifying and ensuring consistency of deeply nested configuration data entered using different means. However, in environment with less experience, I have seen "Akira-final-scene-tetsuo" monsters come to life with this. Another loosely related suggestion: if we could stay away from singleton as examples, I t…

I enjoyed reading through the Django ORM code to understand how it worked. Metaclasses in python are fun, but I can rarely justify actually using them for anything other than messing around. Oh but how I wish I needed to use them more!

Re: Python metaclasses

#5

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?)

The Django ORM or sqlalchemy good starting points. Metaclasses are what make them tick.

Re: Python metaclasses

#6

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?)

The registry really does seem to be the most common use case:

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.

Re: Python metaclasses

#7

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?)

I've used metaclasses to implement debug logging, where every class method logs its parameters and return values:

https://github.com/bkeroack/elita/blob/27129ea64382275effa99...

https://github.com/bkeroack/elita/blob/27129ea64382275effa99...

Re: Python metaclasses

#8

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?)

I'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.

Re: Python metaclasses

#9

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?)

I've had fun using them to make quasi-orms for CSV and Excel spreadsheet data before.

Re: Python metaclasses

#10

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?)

I've used it to simulate new language features. It sort of allows you to program the type system.
Post reply on HN