Editor settings

General settings

Save settings

Learn to program with Python

Creating an object from a class #

Now that we have our own class, we can create a new object of that type by calling the class, just like calling a function:

class Car:
    pass

car_object = Car()
print(type(car_object)) # <class '__main__.Car'>

When we call the function type() with the car_object as an argument, we can see that the type of that object is now __main__.Car, which tells us that the object is type Car, defined in the file being run (__main__).


Exercise #

  • Create a new object person from the Person class.

Tests #

  • Variable person must be an instance of the Person class
Output will be displayed here