Python class is concept of “object oriented programming”. Python is an object oriented programming language (oop). OOP is a way to build software.
With OOP you can make your program much more organized, scalable, reusable and extensible. The OOP concept can be a bit weird. It can be challenging to grasp, but it’s a very powerful concept.
Related course: Complete Python Bootcamp: Go from zero to hero in Python
In Python, you can define objects. An object is a collection of methods and variables. Objects live somewhere in the computers memory. They can be manipulated at runtime.
Lets create a theoritcal example, we create an object dog. Creating an object is just one line of code:
obj1 = dog()
Each object can have variables. The values of those variables are unique to the object. We set object variables (name,age)
obj1.name = "Woof"
If methods exist for an object, they can be called. The objects unique variables can be used in those methods.
The methods can be used multiple times:
In your program you can have multiple objects. Those objects can be of the same type or a different type.
obj1 = dog()
So how does Python know the type of an object? How does it know which methods and variables exist for a type? They are defined in a class.
Objects are always created from classes. A class define each method and variable that exists within the object. You could see classes as blueprints for objects.
Remember we had objects of type dog in the previous example?
The object had variables (age,name) and a method (bark). they are defined in the class dog.
This is how that class is defined:
First we define the class itself: class dog. Then the variables are defined (name,dog). Finally we define the method. If you look closely you’ll see the method has the word self in it. The word self refers to the object (You can create several objects from a class.)
Classes are not just used for funny examples (dog,bird). They’re used all over computer software.
If you are given the task of making a web browser, you need to show a website at some point.
Lets say the program will be object orientated. Then a class can be defined in this way:
Wait.. what’s init?
If an object is created, the method init is called. This is always the first method that is called when creating a new object. The method is called the constructor.
Then you can create the web browser object.
In this example we have one object (obj), created from the class Website. The class has two methods: init() and showTitle().
Try the exercises below
- Can you have more than one class in a file?
- Can multiple objects be created from the same class?
- Can objects create classes?
- Using the code above, create another object
- Add a method to the class: location()
After completing these continue with the next exercise.