views:

1842

answers:

3

I have read posts like these:

  1. What is a metaclass in Python?
  2. What are your (concrete) use-cases for metaclasses in Python?
  3. Python's Super is nifty, but you can't use it

but somehow I got confused, many confusions like

when and why i would have to do something like this

#refer link1
return super(MyType, cls).__new__(cls, name, bases, newattrs)

or

#refer link2
return super(MetaSingleton, cls).__call__(*args, **kw)

or

#refer link2
return type(self.__name__ + other.__name__, (self, other), {})

how does super work exactly?

what is class registry and unregistry in link1 and how it exactly works? (I thought it has something to do with singleton, I may be wrong, being from C background, my coding style is still a mix of functional and OO).

Can someone explain the flow of class instantiation (subclass, metaclass, super, type) and method invocation (

metaclass->__new__, metaclass->__init__, super->__new__, subclass->__init__ inherited from metaclass

) with a well commented working code (though the first link is quite close, but does not talk about cls keyword and super(..) and registry). Preferably an example with multiple inheritance.

P.S.: made the last part as code because SO formatting was converting the text metaclass->__new__ to metaclass->new

For experts here: please feel free to correct the question if there is a snag.

+8  A: 
Alabaster Codify
Thanks. Registry and MetaSingleton - are just some names from the link (http://stackoverflow.com/questions/100003/what-is-a-metaclass-in-python)i mentioned, the context is in code there. This does not answer all questions, I'll wait for a few more answers before I revert back on any.
JV
Ah, I see - I didn't understand what the #first link and #second link comments meant. I've added explanations for the samples now that I can see them in context.
Alabaster Codify
Thanks again. I have edited my question to make link references explicit.
JV
+7  A: 

Here's the more pragmatic answer.

It rarely matters

  1. "What is a metaclass in Python". Bottom line, type is the metaclass of all classes. You have almost no practical use for this.

    class X(object):
        pass
    type(X) == type
    
  2. "What are your (concrete) use cases for metaclasses in Python?". Bottom line. None.

  3. "Python's Super is nifty, but you can't use it". Interesting note, but little practical value. You'll never have a need for resolving complex multiple inheritance networks. It's easy to prevent this problem from arising by using an explicity Strategy design instead of multiple inheritance.

Here's my experience over the last 7 years of Python programming.

  1. A class has 1 or more superclasses forming a simple chain from my class to object.

  2. The concept of "class" is defined by a metaclass named type. I might want to extend the concept of "class", but so far, it's never come up in practice. Not once. type always does the right thing.

  3. Using super works out really well in practice. It allows a subclass to defer to it's superclass. It happens to show up in these metaclass examples because they're extending the built-in metaclass, type.

    However, in all subclass situations, you'll make use of super to extend a superclass.

Metaclasses

The metaclass issue is this:

  • Every object has a reference to it's type definition, or "class".

  • A class is, itself, also an object.

  • Therefore a object of type class has a reference to it's type or "class". The "class" of a "class" is a metaclass.

Since a "class" isn't a C++ run-time object, this doesn't happen in C++. It does happen in Java, Smalltalk and Python.

A metaclass defines the behavior of a class object.

  • 90% of your interaction with a class is to ask the class to create a new object.

  • 10% of the time, you'll be using class methods or class variables ("static" in C++ or Java parlance.)

I have found a few use cases for class-level methods. I have almost no use cases for class variables. I've never had a situation to change the way object construction works.

S.Lott
Perhaps I should have added that the real-world uses of meta-classes are few and far between, I definitely agree with that! The notable exceptions do justify meta-classes existence, however.
Alabaster Codify
On multiple inheritance, I disagree. A simple chain of stand-alones up to object is good design IMO, but mix-ins are great and I'd encourage their use when appropriate. And as soon as you use mix-ins, you really do need to understand the MRO to make sure you don't shoot yourself in the foot.
Alabaster Codify
@jamesbrady: metaclasses have to exist; no issue there. Do they have to be deeply understood? Not really.
S.Lott
@jamesbrady: as soon as you need to understand MRO, you've made a mistake. I use mixins without any MRO issues because I design the mixins to be very simple.
S.Lott
@S.Lott: I think a decent understanding of the MRO and super is a very valuable tool. Without being aware of the nastiness that multiple inheritance *can* leave you with, we wouldn't know why linear inheritance chains, simple mix-ins and so on are desirable. Knowing the wrong option is still useful!
Alabaster Codify
@jamesbrady: My answer is based on my experience: it so rarely comes up in practice that I don't see any value in deep understanding. A superficial summary is all anyone needs. That's why I posted my answer.
S.Lott
@S.Lott: 1. "It rarely matters" - can you elicit the rare cases2. "Do they have to be deeply understood?"- why not? atleast for the curiosity part of it, even if one can't find more than a handful use cases. 3.I agree with Strategy design part of it but using MRO as a cross check is fine I guess.
JV
@JV: I have never used a metaclass and never will. For me, it does not matter. I say "rarely" because I have seen it used, for example, in Django. Read their models module for an example.
S.Lott
@JV: "why not deeply understand this?" Deep understanding should be reserved for end users' use cases. Edge-of-the-envelope language features need a just-enough understanding.
S.Lott
+1 for a clear practical answer
Maddy
+2  A: 

A very useful link http://cleverdevil.org/computing/78/ found it lately, so posting it for interest of all. It came on Reddit actually.

JV