Created
July 10, 2018 14:27
-
-
Save tal95shah/5ae07dc33ff6ddb1c5011685877bcd48 to your computer and use it in GitHub Desktop.
Sortable Dataclasses with field objects.
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
| from dataclasses import dataclass,field | |
| @dataclass(frozen=True,order=True) | |
| class Employee(object): | |
| id: int = field(compare=True) | |
| name: str = field(compare=False) | |
| salary: int = field(compare=True) | |
| if __name__ == '__main__': | |
| emp = Employee(1,"Talha Gillani",5000) | |
| emp2= Employee(100,"Syed Talha Gillani",10000) | |
| employeeList=[emp2,emp] | |
| print("EmployeeList:{}".format(employeeList)) | |
| employeeList.sort() | |
| print("EmployeeList:{}".format(employeeList)) | |
| print("emp:{}".format(emp)) | |
| print("emp2:{}".format(emp2)) | |
| print("emp == emp2:{}".format(emp == emp2)) | |
| print("emp != emp2:{}".format(emp != emp2)) | |
| print("emp >= emp2:{}".format(emp >= emp2)) | |
| print("emp <= emp2:{}".format(emp <= 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