Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python get class name

Tags:

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.

like image 217
Low Lifer Avatar asked Jan 24 '13 23:01

Low Lifer


People also ask

How do I get classes in Python?

The first and easiest method to get a class name in python is by using __class__ property which basically refers to the class of the object we wish to retrieve. Here we combine the property with __name__ property to identify the class name of the object or instance.

How do I find the instance name of a Python?

Example 2: Using type() and __name__ attribute Using attribute __name__ with type() , you can get the class name of an instance/object as shown in the example above. type() gives the class of object v and __name__ gives the class name.

How do you print a class type in Python?

How to Print the Type of a Variable in Python. To get the type of a variable in Python, you can use the built-in type() function. In Python, everything is an object. So, when you use the type() function to print the type of the value stored in a variable to the console, it returns the class type of the object.

How do I find the class name of an object?

If you have a JavaSW object, you can obtain it's class object by calling getClass() on the object. To determine a String representation of the name of the class, you can call getName() on the class.


2 Answers

Since python 3.3, you can use the __qualname__ variable:

class TestClass():
    myName = __qualname__
like image 195
Aran-Fey Avatar answered Oct 14 '22 14:10

Aran-Fey


Just use a method?

class TestClass(object):
    @classmethod
    def get_save_directory(cls):
        return os.path.join(save_dir, cls.__name__)

Or you could just have a class attribute indicating the save directory; less magic is usually a good thing. Plus you could change the name of the class later without breaking your storage.

Also, what? Gigabytes?!

like image 32
Eevee Avatar answered Oct 14 '22 13:10

Eevee