class __init__ (not instance __init__)

Posted by wallacoloo on Stack Overflow See other posts from Stack Overflow or by wallacoloo
Published on 2010-04-24T22:32:30Z Indexed on 2010/04/24 22:33 UTC
Read the original article Hit count: 646

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):
    def __init__(self):
        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()

© Stack Overflow or respective owner

Related posts about python

Related posts about class