Live data from Hacker News

Python metaclasses

ivansmirnov.io

11–20 of 30 posts

Re: Python metaclasses

#12

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

Here's one good example: abc (abstract base classes) from Python's standard library -- https://hg.python.org/cpython/file/3.4/Lib/abc.py

Re: Python metaclasses

#13
This topic has been beat to death. I guess there are more articles explaining metaclasses then people who understand them. There are 11K results for /understanding python metaclasses/ in google.

Re: Python metaclasses

#15

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

Re: Python metaclasses

#17
This seems to still miss the critical mechanism that metaclasses provide to the language.

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

Re: Python metaclasses

#19
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…

"Great tool, but not to be put in the wrong hands."

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".

Re: Python metaclasses

#20

Tangential question, is there anything similar to Moose (Perl) in the Python world?

Caveat: I haven't written any Perl except for an occasional one-liner in a decade.

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.

Post reply on HN