Created
July 10, 2018 13:43
-
-
Save tal95shah/f96042893472457c8ab710a0c756833b to your computer and use it in GitHub Desktop.
Python 3.6 way to code employee class
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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