Hello everybody,
It’s Michael, and today’s Python lesson will be on classes & objects in Python.
Python, like Java, is an object-oriented programming language. Everything in Python is an object, and each object in Python has its own attributes and methods (or functions in Python lingo). Classes in Python serve as object-builders (or blueprints for creating objects) just like classes in Java.
How do you create a class in Python? Let’s find out:
To create a class in Python, you simply need to use the keyword class. That’s it! No need to write public or private or anything like that in front of the class keyword like you would need to do in Java.
Alright, now let’s create an object of the class:

Creating an object of a Python class is similar to creating an object of a Java class, except in Java you would need the new keyword in front of the class name. Pretty simple right?
Now, I just demonstrated the simplest form of classes and objects. These examples wouldn’t serve you well when you’re working on any sort of Python programming project; classes and objects are more complex than what I just discussed.
To better understand classes and objects in Python, we need to understand the __init__ function. The __init__ function is built into every Python class, and it allows you to assign values to objects. Here’s an example of a class with the __init__ function:
In this class, the __init__ function has 4 parameters-self, year, month, and day. Year, month, and day are the variables for the RandomDate class.
There is also another parameter that I haven’t yet discussed-self. The self parameter refers to the class itself; it is used to access variables of the class, which in turn allows you to assign values to these variables. In the example above, I am able to access the year, month, and day variables by using self.year, self.month, and self.day, respectively.
- Remember that in order to access the variables in the
__init__function, use this syntax:self.[variable name] = [variable name]
- Remember that in order to access the variables in the
__init__function, use this syntax:self.[variable name] = [variable name]
You don’t absolutely have to call the first parameter self; you can name it whatever you want but it must be the first parameter of the function. Using the example above, let’s see what happens when we use another variable name instead of self:
This time, I replaced self with thedate but the code-block executed just fine since I remembered to use thedate as the first parameter.
Now, let’s add a method to class. Let’s also create a new object of the class and call the new method we created:
I added the function today to the RandomDate class and created a new object of the RandomDate class-r. The object r consists of the three main parameters of the RandomDate class-year, month, and day. The self (or thedate in this case) parameter isn’t included in the object creation, as it is only used to access the three main parameters.
Once I create the r object of the RandomDate class, I call the today function and get the output Today is September 20, 2020.
- As you can see above, the three main parameters-
2020,September, and20-are all Strings. This is because you can only concatenate Strings with other Strings in Python. Just something to keep in mind when you’re writing your Python code.
Now that we have created an object of a class, let’s see how we can modify that object. First, let’s modify one of the properties of r:
In this example, I changed the value of day to 21 (in String format), called the today function again, and got the output Today is September 21, 2020.
Now, let’s say we wanted to get rid of the year. How do we do that? Take a look:
To delete an attribute from an object, follow this easy syntax: del [object name].[attribute to be deleted].
OK, so what if you wanted to get rid of the entire r object? This is what you would do:
To delete an entire object, just follow this very easy syntax: del [object name].
The last point I wanted to bring up regarding Python classes and objects is that, just like Python functions, they can’t be empty. However, the same empty-function workaround can be used for empty classes-simply using the keyword pass in the body of the function:
Thanks for reading,
Michael