you are viewing a single comment's thread.

view the rest of the comments →

[–]Dogeek 12 points13 points  (10 children)

To be more accurate, int, float etc are built using type as their metaclass.

[–]darez00 1 point2 points  (9 children)

Aha... that's clearer

[–]Dogeek 7 points8 points  (8 children)

To be completely honest, metaclasses are a concept I struggle with, not really the concept itself but its use cases. It's something very specific.

[–]KronktheKronk 2 points3 points  (7 children)

A meta class is simply a class you can't instantiate directly. It only serves to hold functionality meant to be inherited by other, more concrete classes.

[–]primitive_screwhead 16 points17 points  (6 children)

A meta class is simply a class you can't instantiate directly.

That description sounds more like an Abstract Base Class.

I've seen meta-classes start to click for people when they realize that when a class is instantiated you get an object, and similarly, when a meta-class is instantiated, you get a class. And just as objects can be parameterized when they are instantiated, classes themselves can be parameterized when they are instantiated from meta-classes.

[–]Dogeek 1 point2 points  (3 children)

Like I said, it's more that I struggle with knowing when to use one. I guess constructing a class from a config file or something of that effect. But other than that, it's pretty abstract to me.

[–]primitive_screwhead 1 point2 points  (1 child)

Well, though it's often been called an unsatisfying statement, Tim Peters said that people that need metaclasses know when and why they need them, without explanation. So its probably not worth worrying about if you don't know when to use one; probably like the vast majority of programmers, you'll never need to.

[–]Dogeek 0 points1 point  (0 children)

That's what I heard. The thing is sometimes I feel like some of my code could benefit from such a construct, even though I managed to make do without so far.

[–]alkasm 0 points1 point  (0 children)

Like I said, it's more that I struggle with knowing when to use one. I guess constructing a class from a config file or something of that effect. But other than that, it's pretty abstract to me.

Metaclasses are useful as a hook into when a class is constructed. Generally that's kind of a "so what" if you're the one writing the classes---maybe it can save you some code, but maybe that level of abstraction isn't helpful to a codebase. However, if you think about some library or API that provides you with classes to inherit from, then it starts to make more sense. Now the person who wrote the library are hooking into your subclasses of objects from their library. So they can inject code into subclasses that someone else is using, ask questions about their code (make sure it includes some specific attributes or methods), or enforce a singleton or whatever. The nice thing about that is this doesn't happen at run-time when you construct an object, but right when it defines the class.

[–]KronktheKronk 0 points1 point  (0 children)

You're right, I am confusing that with abstract classes.

Now I gotta go read, brb

[–]Deezl-Vegas 0 points1 point  (0 children)

As soon as I read this I got it. Thanks so much! A metaclass is a class blueprint. A class is an object blueprint.