Sobes.tech
Junior — Middle

How is a class instance created in Python?

sobes.tech AI

Answer from AI

In Python, creating an instance of a class is done by calling the class name as a function. The init method is called if it is defined, to initialize the object.

Example:

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

# Creating an instance of the class
person = Person("Ivan", 30)
print(person.name)  # Outputs: Ivan

Thus, person is an object of the Person class with the specified attributes.

How is a class instance created in Python? — Python - sobes.tech