How to Get Class Name in Python

Is there a way to get the Python class name of an object? It’s a relatively simple process that can be done in just a few steps. On this page, there are different methods are demonstrated to get the class name in Python so that you can make use of it in your python projects or resolve your Python-related problems. Within a few steps, you can get the name of a class in Python by following this tutorial. So let’s get on the board!

If you want to learn more about Python Programming, visit Python Programming Tutorials

using format() method in aggregation with __class__.__name__ approach to get class name in python

So here is the entire process that revolves around getting the class name in Python. The __name__ variable in the Python program provides the name of the module on which we are working.

Using __class__.__name__  approach, you can display the name of a class by creating the object for that class. Here in the following example code class object “url” is required to display the class name. 

Here is the execution process goes like:

  • Class names are defined as “www” when creating a class. 
  • A variable “name” will be created in the class’s init constructor.
  • Afterward, use the (.) operator to get at the variable. Always-public properties are commonly accessed using the (.) operator.
  • In this case, the function is a “site”, which takes as its parameter an instance of the class “self”. 
  • A class attribute’s value is returned by the return expression. Here in the following example, the format() function is used to display the class name. Within curly braces, the index order is assigned to display the complete url of the website. {1} becomes class name which is “www”, and {0} becomes the ‘com’. 
  • A final step would be to initialize the new variable, “url,” for the class and pass the value for an instance for the class, which is ‘com’, 
  • Then invoke the site() method using the operator (.) and the constructor variable “name”.
class www(object):
   def __init__(self, name):
       self.name = name
   def site(self):
       return "{1}.Entechin.{0}".format(self.name,self.__class__.__name__)
url=www('com')
print("class argument :", url.name)
print("url :", url.site())
class argument : com
url : www.Entechin.com

Using __class__.__name__ to get a class name in python

There are two ways to get the class name one is using __class__.__name__

And the other is __class__. Below are a demonstration of using both methods separately to get the class name in Python. 

Here in the example below, the process executes in the following manners:

  • First initializing the class, with the name “url”.
  • The class’s constructor  __init__ will create a variable named “name”. And using it with an instance of class “self”.
  • Create an object for the class and store it into a new variable, and pass the value to the class instance in the parameter. 
  • Print command will display the data type (which is a class) using the  __class__ method and display the class name.
  • The other print function will help us to access the class name, and display “url” at the output. 
class url:
   def __init__(self, name):
       self.name = name
url = url("www.Entechin.com")
print (url.__class__)
print (url.__class__.__name__)
<class '__main__.url'>
url

Here in the following example, It is impossible for a class definition to be empty, but if you have a class definition with no content, use the pass statement to avoid an error. The pass keyword is itself a complete statement that helps to execute the program execution, otherwise it will rise the error. 

Invoke a class with an empty argument and save the result in a new variable, “url”. The print command with the str() function helps to get the class name in Python. 

class URL:
   pass
url = URL()
print(str(url.__class__))
<class '__main__.URL'>

Using type() and __name__ approach to get class name in python

The type () is the easiest way to access and search the class name in Python. Firstly, the simple program in the example code shows how to get the type of variable with __name__ using the (.) operator. As mentioned above, __name__ displays the name of the module, on which we are working on. 

Here in the simple examples, the string is saved in the new variable “url”. Now using the type() function in aggregation with __name__ using the (.) operator to get the type of class name in Python. 

Here it will return “str” as output, as we pass a string in the program body. 

url = "www.entechin.com"
print(type(url).__name__)
str

Here in the following program, the code executes as:

  • Creating a class with a class name “url”.
  • Defining a class constructor  __init__ as a function within the class and passing them a variable ‘name’, as a parameter and using them to the instance of the class ‘self’. 
  • Now create an object variable for the class.
  • Invoking class object with __class__ using (.) will return the data type with class name as output. 
  • Invoke the type() function with  __name__ to assess the class name.
class url:
   def __init__(self, name):
       self.name = name
url = url(1)
print (url.__class__)
print (type(url).__name__)
<class '__main__.url'>
url

__qualname__ attribute using nested classes to access the class name in Python

The execution process executes in the following steps: 

  • Defining two classes ‘www’, and ‘Entechin’. 
  • Create variable instances for both classes. 
  • The ‘self’, a class instance, is used within the __init__ constructor variables ‘name’, and ‘a’. The __init__ constructor is defined for the class ‘www’.
  • Now define a __init__ constructor for class ‘Entechin’, and pass the variable ‘domain’, and use it with ‘self’, a class instance. 
  • In the class “www”, a variable “a” is passed as a parameter to the Entechin class object.
  • Next, the class “www” object “WWW” is created where the values for the instances of the “www” class are set. 
  • There is a method “__name__” for getting the class name as well as a method “__qualname__” for getting the class name of each class.
class www:
   def __init__(self, name, a):
       self.name = name
       self.domain = self.Entechin(a)
   class Entechin:
       def __init__(self, domain):
           self.domain = domain
WWW = www("Entechin",'.com')
print(WWW.domain.__class__.__name__)
print(WWW.domain.__class__.__qualname__)
Entechin
www.Entechin

Final words

On this page, there is a detailed discussion on how to get the class name in Python. However, there are many possibilities to access the class name in Python, but this page covers almost all the targeted approaches to accessing the class name in Python. These includes __name__, __class__.__name__, type(), and __class__.__qualname__ all with simple example codes with step-by-step demonstration of source code. 

Leave a Comment

Your email address will not be published. Required fields are marked *