How do I get a reference for the current class object?
Question: In Python, how do I get a reference to the current class object within a class
statement? Example:

    
    
    def setup_class_members(cls, prefix):
        setattr(cls, prefix+"_var1", "hello")
        setattr(cls, prefix+"_var2", "goodbye")
    
    class myclass(object):
        setup_class_members(cls, "coffee")  # How to get "cls"?
    
        def mytest(self):
            print(self.coffee_var1)
            print(self.coffee_var2)
    
    x = myclass()
    x.mytest()
    
    >>> hello
    >>> goodbye
    

  
Alternatives that I've written off are:

  1. Use `locals()`: This gives a dict in a class statement that can be written to. This seems to work for classes, however the documentation tells you not to do this. (I might be tempted to go with this alternative if someone can assure me that this will continue to work for some time.)

  2. Add members to the class object after the `class` statement: My actual application is to derive a PyQt4 `QWidget` class with dynamically created `pyqtProperty` class attributes. `QWidget` is unusual in that it has a custom metaclass. Very roughly, the metaclass compiles a list of `pyqtProperties` and stores it as additional member. For this reason, properties that are added to the class after creation have no effect. An example to clear this up:

    
    
    from PyQt4 import QtCore, QtGui
    
    
    # works
    class MyWidget1(QtGui.QWidget):
        myproperty = QtCore.pyqtProperty(int)
    
    
    # doesn't work because QWidget's metaclass doesn't get to "compile" myproperty
    class MyWidget2(QtGui.QWidget):
        pass
    MyWidget2.myproperty = QtCore.pyqtProperty(int)
    

Please note that the above will work for most programming cases; my case just
happens to be one of those unusual corner cases.


Answer: AFAIK there is two way to do what you want:

  1. Using [metaclass](http://docs.python.org/reference/datamodel.html#customizing-class-creation), this will create your two variables in class creation time (which i think is what you want):
    
        class Meta(type):
        def __new__(mcs, name, bases, attr):
            prefix = attr.get("prefix")
            if prefix:
                attr[prefix+"_var1"] = "hello"
                attr[prefix+"_var2"] = "goodbye"
    
            return type.__new__(mcs, name, bases, attr)
    
    class myclass(object):
        __metaclass__ = Meta
        prefix = "coffee"
    
        def mytest(self):
            print(self.coffee_var1)
            print(self.coffee_var2)
    

  2. Create your two class variable in instantiation time:

class myclass(object): prefix = "coffee"

    
         def __init__(self):     
         setattr(self.__class__, self.prefix+"_var1", "hello")
         setattr(self.__class__, self.prefix+"_var2", "goodbye")
    
     def mytest(self):
         print(self.coffee_var1)
         print(self.coffee_var2)
    

N.B: I'm not sure what you want to achieve because if you want to create
dynamic variables depending on the `prefix` variable why are you accessing
like you do in your `mytest` method ?! i hope it was just an example.

