Python – How to Get Class Name

python

So i have this problem, i want to get the name of a python class in this way:

class TestClass():
    myName = (get class name here automatically)
    saveDirectory = os.path.join(saveDir, myName) # so i can save it in unique location
    def __init__(self):
        pass # do something

However, it seems that __class__.__name__ doesn't actually exist as of yet at the time when myName is created. So i have been forced to put that into the __init__() function, like this:

class TestClass():
    def __init__(self):
        self.myName = self.__class__.__name__
        saveDirectory = os.path.join(saveDir, self.myName) # so i can save it in unique location
        pass # do something

But this has a big problem, because I cannot get the name of the class until I instantiate the class, I instend to create a few gigabytes of data for each instance of the class to be saved in the saveDirectory, so it can be re-used later. So I don't actually want to do go ahead with my current solution.

Is there anyway to get the class name as I have intended? Or am I just dreaming?

EDIT:

Thanks guys for your great suggestions. I am going to spend a little bit of time taking a look at Metaclasses. Otherwise, I will probably create a dictionary globally, and have references to these instantiated classes instead.

Best Answer

Since python 3.3, you can use the __qualname__ variable:

class TestClass():
    myName = __qualname__
Related Question