Programming in Python and object-oriented is a combination that can give good results and be used for many different types of applications. One of the basic concepts of guided programming is the concept of classes, in today's post we are going to get our hands dirty and implement a simple class in Python and display some of its attributes on screen.
This is a fundamental concept not only to understand the code that we are going to implement in this post, but also to develop object-oriented in general.
So let's get down to business, hands on the code!
Step 1: Defining the basic structure.
After defining the location of your application where you will store your class, you can start by assembling the base structure of a class as indicated below:
class Car:
def __init__(self):
To exemplify here we will create a class to represent a car. In the code snippet shown above we have the reserved word "class" always followed by the name of our class with the first capital letter. "Identified" within the scope of the class we have already defined the initialization method of this class (__init__) and from this structure we can go to the next step.
Step 2: Defining the attributes of this class.
We can assign predefined values to the attributes of our class, however, let's go a step further and, in addition to a predefined attribute, define 3 more dynamic attributes as shown below:
class Carro:
def __init__(self, color, price):
self.color = color
self.price = price
self.number_of_doors = 4
At first this definition of "self" may seem confusing, but a little later when we instantiate an object of our class it will become clearer. We can immediately establish the following definition, the color and price attributes are attributes to be defined when the object is created and the number_of_doors attribute is predefined as 4.
Summarizing the code above, we define the four attributes of our class. Now, we can instantiate our objects to start working with them.
Step 3: Creating an object (instantiating the class).
Instantiating our class is the easiest step in this whole story, as shown below.
car_A = Car("Red", 150000)
We instantiate our class by assigning an object of type "Car" to a variable, passing the attributes in the sequence in which we defined them in our class (color and price). That done we have a variable called in this case "car_A" which is an object of class Car.
Step 4: Returning values from this object.
Now it's easy, with our object ready we can return the values as shown below.
print("Color: " + str(car_A.color) + ", Number of doors: " + str(car_A.number_of_doors) + ".")
Here we show all the attributes of our object on screen, you can work with them individually or not. Finally, our complete code is as shown below:
The entire code:
class Car:
def __init__(self, color, price):
self.color = color
self.price = valor
self.number_of_doors = 4
car_A = Car("Red", 150000)
print("Color: " + str(car_A.color) + ", Number of doors: " + str(car_A.number_of_doors) + ".")
If we disregard the line breaks, in a total of 10 lines we have a class implemented, with 3 attributes defined and with them already being shown on screen. This reflects a lot of the practicality not only of the Python language but also of working with object-oriented programming.
Comments