Skip to content

Commit 80de485

Browse files
committed
Add section on default object comparison
1 parent 893089e commit 80de485

File tree

1 file changed

+199
-3
lines changed

1 file changed

+199
-3
lines changed

source-code/object-orientation/attr_intro.ipynb

Lines changed: 199 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -914,13 +914,209 @@
914914
" print(f'no luck with {guess}')"
915915
]
916916
},
917+
{
918+
"cell_type": "markdown",
919+
"id": "b4a1f9ec-2403-4c86-b41d-b3b22044bc45",
920+
"metadata": {},
921+
"source": [
922+
"## Comparisons"
923+
]
924+
},
925+
{
926+
"cell_type": "markdown",
927+
"id": "29040163-48b6-4529-86ad-bb9b6488c8d1",
928+
"metadata": {},
929+
"source": [
930+
"By default, `attrs` will construct comparison methods for you, i.e., `__eq__`, `__neq__`, but also `__lt__` and so on."
931+
]
932+
},
933+
{
934+
"cell_type": "code",
935+
"execution_count": 18,
936+
"id": "d3d21db1-0858-4929-b48a-1df7d69bbb56",
937+
"metadata": {},
938+
"outputs": [],
939+
"source": [
940+
"@attr.s\n",
941+
"class Person:\n",
942+
" lastname: str = attr.ib()\n",
943+
" firstname: str = attr.ib()\n",
944+
" age: int = attr.ib()\n",
945+
" \n",
946+
" @age.validator\n",
947+
" def age_validator(self, attribute, value):\n",
948+
" if value < 0 or value >= 130:\n",
949+
" raise ValueError(f'age should be between 0 and 130')"
950+
]
951+
},
952+
{
953+
"cell_type": "markdown",
954+
"id": "c3567e93-7d09-475c-a225-c482095f829e",
955+
"metadata": {},
956+
"source": [
957+
"The natural order for objects of this class is lexicographic on lastname, firstname, and the numeric on age. Note that the declaration order of the attributes matters and determines the final sort order."
958+
]
959+
},
960+
{
961+
"cell_type": "code",
962+
"execution_count": 24,
963+
"id": "876cfb7a-1697-4bd4-bd78-edd8b5ab8afb",
964+
"metadata": {},
965+
"outputs": [
966+
{
967+
"data": {
968+
"text/plain": [
969+
"[Person(lastname='Zosimo', firstname='Alice', age=43),\n",
970+
" Person(lastname='Leibovitch', firstname='Aaron', age=31),\n",
971+
" Person(lastname='Leibovitch', firstname='Robert', age=49),\n",
972+
" Person(lastname='Zosimo', firstname='Alice', age=25)]"
973+
]
974+
},
975+
"execution_count": 24,
976+
"metadata": {},
977+
"output_type": "execute_result"
978+
}
979+
],
980+
"source": [
981+
"people = [\n",
982+
" Person(firstname='Alice', lastname='Zosimo', age=43),\n",
983+
" Person(firstname='Aaron', lastname='Leibovitch', age=31),\n",
984+
" Person(firstname='Robert', lastname='Leibovitch', age=49),\n",
985+
" Person(firstname='Alice', lastname='Zosimo', age=25),\n",
986+
"]\n",
987+
"people"
988+
]
989+
},
990+
{
991+
"cell_type": "markdown",
992+
"id": "bcf35d6a-596b-4c00-8584-9ba0319d1b10",
993+
"metadata": {},
994+
"source": [
995+
"We can no use Python's `sorted` function to sort the people in this list according to lastname, firstname and age."
996+
]
997+
},
998+
{
999+
"cell_type": "code",
1000+
"execution_count": 23,
1001+
"id": "82693535-6016-483d-8676-6e87105db263",
1002+
"metadata": {},
1003+
"outputs": [
1004+
{
1005+
"data": {
1006+
"text/plain": [
1007+
"[Person(lastname='Leibovitch', firstname='Aaron', age=31),\n",
1008+
" Person(lastname='Leibovitch', firstname='Robert', age=49),\n",
1009+
" Person(lastname='Zosimo', firstname='Alice', age=25),\n",
1010+
" Person(lastname='Zosimo', firstname='Alice', age=43)]"
1011+
]
1012+
},
1013+
"execution_count": 23,
1014+
"metadata": {},
1015+
"output_type": "execute_result"
1016+
}
1017+
],
1018+
"source": [
1019+
"sorted(people)"
1020+
]
1021+
},
1022+
{
1023+
"cell_type": "markdown",
1024+
"id": "b7bf5733-4938-4953-9bff-58132ced8c7d",
1025+
"metadata": {},
1026+
"source": [
1027+
"Equality is also based on the object's attributes."
1028+
]
1029+
},
9171030
{
9181031
"cell_type": "code",
919-
"execution_count": null,
920-
"id": "b84b2436-9a82-4872-a087-71fd4cbe5de4",
1032+
"execution_count": 25,
1033+
"id": "aec2338b-eb7b-4af9-af75-d3c0cf6f62e5",
9211034
"metadata": {},
9221035
"outputs": [],
923-
"source": []
1036+
"source": [
1037+
"alice1 = Person(firstname='Alice', lastname='Zosimo', age=43)\n",
1038+
"alice2 = Person(firstname='Alice', lastname='Zosimo', age=43)\n",
1039+
"alice3 = Person(firstname='alice', lastname='zosimo', age=43)"
1040+
]
1041+
},
1042+
{
1043+
"cell_type": "markdown",
1044+
"id": "90d4fee2-79c6-430f-9b7b-99c49e87c083",
1045+
"metadata": {},
1046+
"source": [
1047+
"`alice1` has the same attributes as `alice2`, but they are distinct objects nevertheless."
1048+
]
1049+
},
1050+
{
1051+
"cell_type": "code",
1052+
"execution_count": 26,
1053+
"id": "e700a830-20e4-4d95-82d8-17faf8208878",
1054+
"metadata": {},
1055+
"outputs": [
1056+
{
1057+
"data": {
1058+
"text/plain": [
1059+
"True"
1060+
]
1061+
},
1062+
"execution_count": 26,
1063+
"metadata": {},
1064+
"output_type": "execute_result"
1065+
}
1066+
],
1067+
"source": [
1068+
"alice1 == alice2"
1069+
]
1070+
},
1071+
{
1072+
"cell_type": "code",
1073+
"execution_count": 27,
1074+
"id": "8a90a20b-f421-4744-a536-06eb8892c41e",
1075+
"metadata": {},
1076+
"outputs": [
1077+
{
1078+
"data": {
1079+
"text/plain": [
1080+
"False"
1081+
]
1082+
},
1083+
"execution_count": 27,
1084+
"metadata": {},
1085+
"output_type": "execute_result"
1086+
}
1087+
],
1088+
"source": [
1089+
"alice1 is alice2"
1090+
]
1091+
},
1092+
{
1093+
"cell_type": "markdown",
1094+
"id": "d32a45c8-0d4e-4f0e-bef8-5e7f1f05751f",
1095+
"metadata": {},
1096+
"source": [
1097+
"`alice3` is different from `alice1`"
1098+
]
1099+
},
1100+
{
1101+
"cell_type": "code",
1102+
"execution_count": 28,
1103+
"id": "ff732001-18bd-44c9-a874-50179a4e9ff1",
1104+
"metadata": {},
1105+
"outputs": [
1106+
{
1107+
"data": {
1108+
"text/plain": [
1109+
"False"
1110+
]
1111+
},
1112+
"execution_count": 28,
1113+
"metadata": {},
1114+
"output_type": "execute_result"
1115+
}
1116+
],
1117+
"source": [
1118+
"alice1 == alice3"
1119+
]
9241120
}
9251121
],
9261122
"metadata": {

0 commit comments

Comments
 (0)