Getting the class name of an instance in Python

Glance on Python ‘Class’

A class in Python is a blueprint for creating objects. It defines a set of attributes and behaviors that objects of the class will have. A class is essentially a template for creating objects, and each object created from the class is called an instance of the class.

Classes allow you to encapsulate data and behavior, and provide a way to structure and organize code. They allow you to create reusable code, making it easier to write, maintain and extend your code.

Here’s a simple example of a class definition in Python:

Python
class Car:
    def __init__(self, make, model, year):
        self.make = make
        self.model = model
        self.year = year
        
    def start_engine(self):
        print("Engine started")
        
    def stop_engine(self):
        print("Engine stopped")

In this example, ‘Car‘ is the class name, ‘make‘, ‘model‘, and ‘year‘ are the attributes of the class, and ‘start_engine‘ and ‘stop_engine‘ are the methods of the class. To create an instance of the ‘Car‘ class, you would call ‘Car()‘ and pass any required arguments, such as the make, model, and year of the car.

Determine which Python ‘class’ is used to generate an instance of an object

To determine the class of an object in Python, you can use the ‘type‘ built-in function. The ‘type‘ function returns the type of the object passed as an argument.

Example:

Python
class MyClass:
    pass

obj = MyClass()
print(type(obj))

Output:

<class '__main__.MyClass'>

You can also use the ‘isinstance‘ function to check if an object is an instance of a specific class or a subclass of it.

Example:

Python
class MyParentClass:
    pass

class MyChildClass(MyParentClass):
    pass

obj = MyChildClass()

print(isinstance(obj, MyParentClass))
print(isinstance(obj, MyChildClass))

Output:

True
True

Create instance of an object

In Python, you can create an instance of a class by calling the class’s constructor method, which is usually the ‘__init__‘ method. Here’s an example:

Python
class Person:
    def __init__(self, name, age):
        self.name = name
        self.age = age

person1 = Person("Rahim", 30)
person2 = Person("Karim", 40)

In this example, we define a class Person with an ‘__init__‘ method that takes two arguments: ‘name‘ and ‘age‘. The method initializes two instance variables, ‘self.name‘ and ‘self.age‘, with the values passed as arguments.

To create instances of the ‘Person‘ class, we simply call the constructor method and pass in the necessary arguments. In this case, we create two instances, ‘person1‘ and ‘person2‘, with different names and ages.

Once you have created an instance of a class, you can access its attributes and methods using the dot notation. For example, you can access the ‘name‘ attribute of ‘person1‘ like this:

Python
print(person1.name)

This will output:

Rahim

Similarly, you can call the ‘__init__‘ method of ‘person1‘ like this:

Python
person1.__init__("Sifat", 50)
print(person1.name, person1.age)

This will output:

Sifat 50

It’s generally not a good idea to call the ‘__init__‘ method directly like this, but it’s possible if you need to change the values of the instance variables after the object has been created.

Overall, creating an instance of a class is a simple process in Python, and allows you to create multiple objects that share the same attributes and methods.

Share The Tutorial With Your Friends
Twiter
Facebook
LinkedIn
Email
WhatsApp
Skype
Reddit

Check Our Ebook for This Online Course

Advanced topics are covered in this ebook with many practical examples.