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

  • Home
  • SEARCH
  • 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 4041140
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T12:50:35+00:00 2026-05-20T12:50:35+00:00

I want to have an instance of class registered when the class is defined.

  • 0

I want to have an instance of class registered when the class is defined. Ideally the code below would do the trick.

registry = {}

def register( cls ):
   registry[cls.__name__] = cls() #problem here
   return cls

@register
class MyClass( Base ):
   def __init__(self):
      super( MyClass, self ).__init__() 

Unfortunately, this code generates the error NameError: global name 'MyClass' is not defined.

What’s going on is at the #problem here line I’m trying to instantiate a MyClass but the decorator hasn’t returned yet so it doesn’t exist.

Is the someway around this using metaclasses or something?

  • 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-20T12:50:35+00:00Added an answer on May 20, 2026 at 12:50 pm

    Yes, meta classes can do this. A meta class’ __new__ method returns the class, so just register that class before returning it.

    class MetaClass(type):
        def __new__(cls, clsname, bases, attrs):
            newclass = super(MetaClass, cls).__new__(cls, clsname, bases, attrs)
            register(newclass)  # here is your register function
            return newclass
    
    class MyClass(object):
        __metaclass__ = MetaClass
    

    The previous example works in Python 2.x. In Python 3.x, the definition of MyClass is slightly different (while MetaClass is not shown because it is unchanged – except that super(MetaClass, cls) can become super() if you want):

    #Python 3.x
    
    class MyClass(metaclass=MetaClass):
        pass
    

    As of Python 3.6 there is also a new __init_subclass__ method (see PEP 487) that can be used instead of a meta class (thanks to @matusko for his answer below):

    class ParentClass:
        def __init_subclass__(cls, **kwargs):
            super().__init_subclass__(**kwargs)
            register(cls)
    
    class MyClass(ParentClass):
        pass
    

    [edit: fixed missing cls argument to super().__new__()]

    [edit: added Python 3.x example]

    [edit: corrected order of args to super(), and improved description of 3.x differences]

    [edit: add Python 3.6 __init_subclass__ example]

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

Sidebar

Related Questions

I have two classes, and want to include a static instance of one class
If you want to develop against WordPress (i.e., have a local instance running on
I have a nested movie clip instance that I want to access. The path
I have an app where I want to link an instance of a model
Okay, I have this program and I don't want more than one instance of
I want to have a class which implements an interface, which specifies the specific
I want to have a select-only ComboBox that provides a list of items for
I want to have a text box that the user can type in that
I want to have a PHP script send a XML formatted string to another
I want to have a web based admin to upload, delete files and folders

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.