class Employee:
pass
emp1 = Employee()
emp2 = Employee()
print("===== OBJECTS =====")
print(emp1)
print(emp2)
emp1.first_name = "Corey"
emp2.last_name = "Schafer"
print("===== OBJECT ATTRIBUTES =====")
print(emp1.first_name)
print(emp2.last_name)
OUTPUT :
===== OBJECTS =====
<__main__.Employee instance at 0x7f7c631996c8>
<__main__.Employee instance at 0x7f7c63199710>
===== OBJECT ATTRIBUTES =====
Corey
Schafer
class Employee:
def __init__(self, first_name, last_name, salary):
self.first = first_name
self.last = last_name
self.pay = salary
self.email = "{}.{}@company.com".format(first_name, last_name)
def full_name(self):
return "{} {}".format(self.first,self.last)
emp1 = Employee("Harvey", "Specter", "1000000")
emp2 = Employee("Donna", "Paulson", "1000000")
print("===== OBJECT ATTRIBUTES =====")
print(emp1.first)
print(emp2.last)
print(emp1.full_name())
print(emp2.full_name())
OUTPUT :
===== OBJECT ATTRIBUTES =====
Harvey
Paulson
Harvey Specter
Donna Paulson
__init__ is the constructor here. It always receives first parameter as the instance which is called by keyword self.
Parameters of constructor and instance variables are named differently on purpose to show the difference of which is which. So basically constructor is just a function which is accepting parameters and assigning the values to the instance variables.
To access instance variables or functions in the class anywhere use it with self.
self.instance_variable
AND self.function_name()
In fact every object function in class will receive first parameter is self which is mandatory.
We can also call object functions by class name and then pass the object as an parameter explicitly like this.
Employee.full_name(emp1)