Think of a class as a blueprint from which objects are created. A blueprint is a detailed photographic print that shows how something will be built.
Think of a blueprint of a house. This blueprint can be considered as a Class. With the same blueprint, we can create several houses, which can be considered as Objects. And each object, or house, built from this blueprint is an instance of that blueprint.
Let's take an 'Employee' class. You can consider this to be a template for the real-world entity employee. This class would have two things associated with it: Properties and Behaviors. The class ‘Employee’ would have certain properties associated with it such as First Name, Last Name, and Salary. Similarly, the class ‘Employee’ would have certain behaviors associated with it such as Create Full Name, Apply Raise, etc.
Objects are specific instances of a class. If ‘Employee’ is our class, then ‘Mario Rossi’, ‘Ashley Brown’ and ‘Gunther Schafer’ would be the specific instances of the class, or in other words, these would be the objects of the class ‘Employee’.
Creating a class and instances of a class
In the example below, we create a class named Employee. Then, we create two objects (emp1 and emp2), or instances, of this class.
class Employee:
pass
emp1 = Employee()
emp2 = Employee()
print(emp1)
print(emp2)
If you print these objects, you get this:
<__main__.Employee object at 0x000002297B231A00>
<__main__.Employee object at 0x000002297B1A8970>
This essentially shows the two objects have been created are unique instances, as they hold different places in memory.
With the class objects in place, we can now manually create instance variables for each employee. Say, for example, we want emp1 and emp2 to have a first name, a last name, an e-mail, and wage. All you need to do is to set the instance variables and pass their unique attributes. Now that each instance variable has its own unique attributes, you can print out the information you want.
class Employee:
pass
emp1 = Employee()
emp2 = Employee()
emp1.fname = 'Mario'
emp1.lname = 'Rossi'
emp1.email = 'Mario.Rossi@company.com'
emp1.wage = 100000
print(emp1.email)
print(emp1.wage)
Output:
Mario.Rossi@company.com
100000
The code looks nice so far, but it's not practical. By having to set each variable manually, not only are we prone to make mistakes, but we are also not taking advantage of the power of classes. Instead, we are going to use a special function called init(). This function is a constructor from which we are going to assign values to object properties at the moment they're created.
class Employee:
def __init__(self,fname,lname,wage):
self.fname = fname
self.lname = lname
self.wage = wage
self.email = f"{fname}.{lname}@company.com"
emp1 = Employee('Mario','Rossi',100000)
emp2 = Employee('Jane','Smith',70000)
print(emp1.email)
print(emp1.wage)
Output:
Mario.Rossi@company.com
100000
When we create our instances of our Employee class, we can pass in the values that we specified in our init() function. Now our init() function takes the instance which we call self. Self must always be the first parameter. Use the init() function to assign values to object properties