Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 998229
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T07:09:38+00:00 2026-05-16T07:09:38+00:00

Today I have come across a surprising definition of a metaclass in Python here

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T07:09:39+00:00Added an answer on May 16, 2026 at 7:09 am

    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’s __metaclass__ in 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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Today while writing some Visual C++ code I have come across something which has
I have come across this roadblock today. Maybe due to the lack of sleep/coffee,
I have come across a situation today which has me wondering about best practices.
today I have coded a test case for my application, to see how transactions
Today we have a windows application that, using an OCX, creates a web page
Today I have such graph. I run it on windows (source: narod.ru ) I
today i have talk with other friend ,he said he has logic programming skill
I've run on a little problem today: I have a JS drop down menu
greetings, today i have started to learn java using netbeans ide. i would like
I'm familiar with using NSLocalizedString() to localize strings, but the problem I have today

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.