Skip to content

Instantly share code, notes, and snippets.

@tal95shah
Created July 10, 2018 13:43
Show Gist options
  • Select an option

  • Save tal95shah/f96042893472457c8ab710a0c756833b to your computer and use it in GitHub Desktop.

Select an option

Save tal95shah/f96042893472457c8ab710a0c756833b to your computer and use it in GitHub Desktop.
Python 3.6 way to code employee class
class Employee(object):
#Constructor
def __init__(self,id:int,name:str)->None:
self.id = id
self.name = name
#info we get when we say print(e) # e = Employee(id,name)
def __repr__(self):
return f'Employee(id={self.id!r},name={self.name!r})'
# operator overloading to check instances' equality
def __eq__(self,other):
# if instance of same type
if other.__class__ == self.__class__:
return (self.id,self.name) == (other.id,other.name)
return NotImplemented;
# !=
def __ne__(self,other):
if other.__class__ == self.__class__:
return (self.id,self.name) != (other.id,other.name)
return NotImplemented;
if __name__ == '__main__':
emp = Employee(1,"Talha Gillani")
emp2= Employee(2,"Syed Talha Gillani")
print("emp:{}".format(emp))
print("emp2:{}".format(emp2))
print("emp == emp2:{}".format(emp == emp2))
print("emp != emp2:{}".format(emp != emp2))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment