views:

108

answers:

1

Today I have come across a surprising definition of a metaclass in Python here, with the metaclass definition effectively inlined. The relevant part is

class Plugin(object):
    class __metaclass__(type):
        def __init__(cls, name, bases, dict):
            type.__init__(name, bases, dict)
            registry.append((name, cls))

When does it make sense to use such an inline definition?

Further Arguments:

An argument one way would be that the created metaclass is not reusable elsewhere using this technique. A counter argument is that a common pattern in using metaclasses is defining a metaclass and using it in one class and then inhertiting from that. For example, in a conservative metaclass the definition

class DeclarativeMeta(type):
    def __new__(meta, class_name, bases, new_attrs):
        cls = type.__new__(meta, class_name, bases, new_attrs)
        cls.__classinit__.im_func(cls, new_attrs)
        return cls
class Declarative(object):
    __metaclass__ = DeclarativeMeta
    def __classinit__(cls, new_attrs): pass

could have been written as

class Declarative(object):  #code not tested!
    class __metaclass__(type):
        def __new__(meta, class_name, bases, new_attrs):
            cls = type.__new__(meta, class_name, bases, new_attrs)
            cls.__classinit__.im_func(cls, new_attrs)
            return cls
    def __classinit__(cls, new_attrs): pass

Any other considerations?

+7  A: 

Like every other form of nested class definition, a nested metaclass may be more "compact and convenient" (as long as you're OK with not reusing that metaclass except by inheritance) for many kinds of "production use", but can be somewhat inconvenient for debugging and introspection.

Basically, instead of giving the metaclass a proper, top-level name, you're going to end up with all custom metaclasses defined in a module being undistiguishable from each other based on their __module__ and __name__ attributes (which is what Python uses to form their repr if needed). Consider:

>>> class Mcl(type): pass
... 
>>> class A: __metaclass__ = Mcl
...
>>> class B:
...   class __metaclass__(type): pass
... 
>>> type(A)
<class '__main__.Mcl'>
>>> type(B)
<class '__main__.__metaclass__'>

IOW, if you want to examine "which type is class A" (a metaclass is the class's type, remember), you get a clear and useful answer -- it's Mcl in the main module. However, if you want to examine "which type is class B", the answer is not all that useful: it says it'smetaclassin the main module, but that's not even true:

>>> import __main__
>>> __main__.__metaclass__
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute '__metaclass__'
>>> 

...there is no such thing, actually; that repr is misleading and not very helpful;-).

A class's repr is essentially '%s.%s' % (c.__module__, c.__name__) -- a simple, useful, and consistent rule -- but in many cases such as, the class statement not being unique at module scope, or not being at module scope at all (but rather within a function or class body), or not even existing (classes can of course be built without a class statement, by explicitly calling their metaclass), this can be somewhat misleading (and the best solution is to avoid, in as far as possible, those peculiar cases, except when substantial advantage can be obtained by using them). For example, consider:

>>> class A(object):
...   def foo(self): print('first')
... 
>>> x = A()
>>> class A(object):
...   def foo(self): print('second')
... 
>>> y = A()
>>> x.foo()
first
>>> y.foo()
second
>>> x.__class__
<class '__main__.A'>
>>> y.__class__
<class '__main__.A'>
>>> x.__class__ is y.__class__
False

with two class statement at the same scope, the second one rebinds the name (here, A), but existing instances refer to the first binding of the name by object, not by name -- so both class objects remain, one accessible only through the type (or __class__ attribute) of its instances (if any -- if none, that first object disappears) -- the two classes have the same name and module (and therefore the same representation), but they're distinct objects. Classes nested within class or function bodies, or created by directly calling the metaclass (including type), may cause similar confusion if debugging or introspection is ever called for.

So, nesting the metaclass is OK if you'll never need to debug or otherwise introspect that code, and can be lived with if whoever is so doing understand this quirks (though it will never be as convenient as using a nice, real name, of course -- just like debugging a function coded with lambda cannot possibly ever be so convenient as debugging one coded with def). By analogy with lambda vs def you can reasonably claim that anonymous, "nested" definition is OK for metaclasses which are so utterly simple, such no-brainers, that no debugging or introspection will ever conceivably be required.

In Python 3, the "nested definition" just doesn't work -- there, a metaclass must be passed as a keyword argument to the class, as in class A(metaclass=Mcl):, so defining __metaclass__ in the body has no effect. I believe this also suggests that a nested metaclass definition in Python 2 code is probably appropriate only if you know for sure that code will never need to be ported to Python 3 (since you're making that port so much harder, and will need to de-nest the metaclass definition for the purpose) -- "throwaway" code, in other words, which won't be around in a few years when some version of Python 3 acquires huge, compelling advantages of speed, functionality, or third-party support, over Python 2.7 (the last ever version of Python 2).

Code that you expect to be throwaway, as the history of computing shows us, has an endearing habit of surprising you utterly, and being still around 20 years later (while perhaps the code you wrote around the same time "for the ages" is utterly forgotten;-). This would certainly seem to suggest avoiding nested definition of metaclasses.

Alex Martelli
Very nice explanation, thanks.
cji
Impressive answer. There is a bit missing in "you're going to rely ???, and serialization".
Muhammad Alkarouri
I am not sure how much of an issue is `pickle`. Pickling seems to work fine [here](http://gist.github.com/524744).
Muhammad Alkarouri
In fact, I would argue that the misleading representation is a bug in Python's representation of nested classes. See [here](http://gist.github.com/524756). Unless nested classes are banned from Python of course.
Muhammad Alkarouri
@Muhammad, a class's repr is the module name, dot, the class name: that's all. You appear to want a different rule, but I think you don't understand the issues (a class has no reference to the function or class, **if any**, wherein its `class` statement, **if any**, was, which avoids creating a useless circular reference). You're right about pickling and the wrong edit I did (that's why I erroneously left in the mention of pickle), editing now to fix.
Alex Martelli
@Alex: as it happens, I knew exactly the issues you are mentioning. The bug as I saw it is that the representation (module.classname) is wrong for all nested classes. Either don't use that representation or ban nested classes. Or argue that it is a known problem but wontfix.
Muhammad Alkarouri
@Muhammad, why would it be a problem when **exactly identical behavior** is no problem for `class` within a function?! Forbidding class statements within functions would be obviously crazy (they're _extremely_ useful), so forbidding class statements within class bodies, as you advocate, would create a horrid irregularity (asymmetry between function and class bodies) and remove functionality (albeit not extremely useful, but some people do prefer to keep things that way) without **any** good reason. The only "known problem" is such unjustified, not-thought-through whines against Python...!-)
Alex Martelli
@Alex: thanks. After the rewriting and comparing with other cases like classes without names it is much clearer now why the representation cannot be changed. Very much appreciated. And thanks for the dig, I wasn't attacking Python.
Muhammad Alkarouri
@Alex: funny. I have been on the Python mailing list for years, asking and [contributing](http://code.activestate.com/recipes/511474-run-function-in-separate-process/) and [reporting bugs](http://bugs.python.org/issue7764). Since I can to SO and a few people decided I whine (saying Python is good, mind you), everybody is taking whatever I say as a whine. I say, good for you SO.
Muhammad Alkarouri
@Muhammad, I didn't see any previous posts of yours (either here, or on c.l.p, which I have not followed for a few years now), so my assessment of your complaint as "whine" was entirely based on your comments in this one thread -- I had no idea that others had previously expressed their opinion the same way based on other posts or comments of yours to SO. Maybe in retrospect you see how e.g. saying that the repr "is wrong" (while you now see why it's that way) could come across that way, esp. given a basically "frozen" language design (so it can't be a "constructive suggestion"). (cont)
Alex Martelli
(cont) but, sorry if I came across as rude -- I don't consider "whine" a rude word, just shorthand for "a somewhat ill-considered complaint that cannot really lead to positive action", but I do realize that not everybody takes it that way, so, apologies. (I could have used the equivalent word in my native language, "mugugno", which carries exactly the right connotations and denotations, but I doubt it would be understandable, even in context;-).
Alex Martelli
@Alex: First let me explain the technical issues. My opinion is based on your old post. I haven't read your edit at that point. In fact, my reply was 2 minutes _before_ you edited your answer to give the explanation. Also, I still see the representation in the form `module.classname` as wrong. It is wrong for many cases which you enumerate in your edited answer. A "constructive suggestion" would be to change the representation to `module:classname` which flags that there are issues here and that `repr(class)` cannot be passed to `eval`. Is this decision not reviewable (frozen)?
Muhammad Alkarouri
@Alex: Otherwise, I guess the issue was a race condition (me replying before your edited comment); and me not knowing that this is a "frozen" language design issue. Is there a list of frozen issues that are not subject to bug reporting or PEP submissions somewhere? By the way, sorry if my expression (that it is a bug) came a bit too harsh. Though I believe that you gave excellent arguments why it shouldn't/couldn't be fixed in your edited answer.
Muhammad Alkarouri
@Muhammad, `repr` cannot be passed to `eval` in _a lot_ of cases. You can definitely try to make a case (on the ideas mailing list, I guess) that it's worth, probably in Python 3.3, breaking a lot of existing 3rd party frameworks (all the ones that lazy-load classes based on the `'module.classname'` strings) for a minor, debatable, purely aesthetical consideration -- to me, that seems a crazy trade-off, and I suspect you'll be laughed out of court if you seriously proposed that backwards-incompatible change to `'module:classname'`, but I guess I could be wrong.
Alex Martelli
@Alex, thanks for the advice. I can't see a lot of frameworks using `module.classname` as a basis for lazy loading without `eval`, but I can see a fair amount of doctests and test suites suffering. I guess I will leave it then.
Muhammad Alkarouri
@Muhammad, for example, Django, by far the most popular Python web framework, uses exactly strings in dot notation in the URL-dispatcher (to lazy-load views) -- views are normally functions, in this case, but such frameworks don't particularly need to care nor worry, nor of course do they use `eval` for lazy-loading (that would be incredibly silly) -- they'll be using `__import__` and `getattr`!-)
Alex Martelli
@Alex, I actually meant frameworks that would depend on the representation being `module.classname`. Frameworks like Django are not affected by changes to `repr` (at least in the use you highlighted in the url dispatcher). So I don't think they tip the `repr` issue one way or the other.
Muhammad Alkarouri