-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6684afc
commit c15a3d5
Showing
1 changed file
with
139 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"Advanced OOP Class Topics\n", | ||
"------------" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"- subclassing built-in types\n", | ||
"- \"new style\" class changes and extensions\n", | ||
"- static and class methods\n", | ||
"- function decorators\n", | ||
"- more" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 16, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"class Set:\n", | ||
" def __init__(self, value=[]):\n", | ||
" self.data = []\n", | ||
" self.concat(value)\n", | ||
" def intersect(self, other):\n", | ||
" res = []\n", | ||
" for x in self.data:\n", | ||
" if x in other:\n", | ||
" res.append(x)\n", | ||
" return Set(res)\n", | ||
" def union(self, other):\n", | ||
" res = self.data[:]\n", | ||
" for x in other.data:\n", | ||
" if not x in res:\n", | ||
" res.append(x)\n", | ||
" return Set(res)\n", | ||
" def concat(self, value):\n", | ||
" for x in value:\n", | ||
" if not x in self.data:\n", | ||
" self.data.append(x)\n", | ||
" def __len__(self): return len(self.data)\n", | ||
" def __getitem(self,key): return self.data[key]\n", | ||
" def __and__(self, other): return self.intersect(other)\n", | ||
" def __or__(self, other): return self.union(other)\n", | ||
" def __repr__(self): return \"Set:\" + repr(self.data)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 17, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"data": { | ||
"text/plain": [ | ||
"[1, 3, 5, 7]" | ||
] | ||
}, | ||
"execution_count": 17, | ||
"metadata": {}, | ||
"output_type": "execute_result" | ||
} | ||
], | ||
"source": [ | ||
"x = Set([1,3,5,7])\n", | ||
"x.data" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 18, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"Set:[1, 3, 5, 7, 4]\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"print(x.union(Set([1,4,7])))" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 19, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"Set:[1, 3, 5, 7, 4]\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"print(x | Set([1,4,7]))" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "Python 3", | ||
"language": "python", | ||
"name": "python3" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": { | ||
"name": "ipython", | ||
"version": 3 | ||
}, | ||
"file_extension": ".py", | ||
"mimetype": "text/x-python", | ||
"name": "python", | ||
"nbconvert_exporter": "python", | ||
"pygments_lexer": "ipython3", | ||
"version": "3.8.5" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 4 | ||
} |