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
personfrom thePersonclass.
Tests #
- Variable
personmust be an instance of thePersonclass
Getting Started with Python
Data Types
Python Functions
Statements in Python
Basic Debugging in Python
Basic Algorithm
Object-Oriented Programming
Error Handling
Intermediate Algorithm
Python Modules