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 743735
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T08:53:52+00:00 2026-05-14T08:53:52+00:00

Here’s a very simple example of what I’m trying to get around: class Test(object):

  • 0

Here’s a very simple example of what I’m trying to get around:

class Test(object):
    some_dict = {Test: True}

The problem is that I cannot refer to Test while it’s still being defined

Normally, I’d just do this:

class Test(object):
    some_dict = {}
    def __init__(self):
        if self.__class__.some_dict == {}:
            self.__class__.some_dict = {Test: True}

But I never create an instance of this class. It’s really just a container to hold a group of related functions and data (I have several of these classes, and I pass around references to them, so it is necessary for Test to be it’s own class)

So my question is, how could I refer to Test while it’s being defined, or is there something similar to __init__ that get’s called as soon as the class is defined? If possible, I want self.some_dict = {Test: True} to remain inside the class definition. This is the only way I know how to do this so far:

class Test(object):
    @classmethod
    def class_init(cls):
        cls.some_dict = {Test: True}
Test.class_init()
  • 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-14T08:53:53+00:00Added an answer on May 14, 2026 at 8:53 am

    The class does in fact not exist while it is being defined. The way the class statement works is that the body of the statement is executed, as a block of code, in a separate namespace. At the end of the execution, that namespace is passed to the metaclass (such as type) and the metaclass creates the class using the namespace as the attributespace.

    From your description, it does not sound necessary for Test to be a class. It sounds like it should be a module instead. some_dict is a global — even if it’s a class attribute, there’s only one such attribute in your program, so it’s not any better than having a global — and any classmethods you have in the class can just be functions.

    If you really want it to be a class, you have three options: set the dict after defining the class:

    class Test:
        some_dict = {}
    Test.some_dict[Test] = True
    

    Use a class decorator (in Python 2.6 or later):

    def set_some_dict(cls):
        cls.some_dict[cls] = True
    
    @set_some_dict
    class Test:
        some_dict = {}
    

    Or use a metaclass:

    class SomeDictSetterType(type):
        def __init__(self, name, bases, attrs):
            self.some_dict[self] = True
            super(SomeDictSetterType, self).__init__(name, bases, attrs)
    
    class Test(object):
        __metaclass__ = SomeDictSetterType
        some_dict = {}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.