diff --git a/01-data-model/data-model.ipynb b/01-data-model/data-model.ipynb index f525899..47e0369 100644 --- a/01-data-model/data-model.ipynb +++ b/01-data-model/data-model.ipynb @@ -28,8 +28,13 @@ }, { "cell_type": "code", - "execution_count": 1, - "metadata": {}, + "execution_count": 2, + "metadata": { + "ExecuteTime": { + "end_time": "2024-03-20T02:38:26.213972Z", + "start_time": "2024-03-20T02:38:26.207687Z" + } + }, "outputs": [], "source": [ "import collections\n", @@ -52,16 +57,19 @@ }, { "cell_type": "code", - "execution_count": 2, - "metadata": {}, + "execution_count": 3, + "metadata": { + "ExecuteTime": { + "end_time": "2024-03-20T02:38:26.278006Z", + "start_time": "2024-03-20T02:38:26.270554Z" + } + }, "outputs": [ { "data": { - "text/plain": [ - "Card(rank='7', suit='diamonds')" - ] + "text/plain": "Card(rank='7', suit='diamonds')" }, - "execution_count": 2, + "execution_count": 3, "metadata": {}, "output_type": "execute_result" } @@ -73,16 +81,19 @@ }, { "cell_type": "code", - "execution_count": 3, - "metadata": {}, + "execution_count": 4, + "metadata": { + "ExecuteTime": { + "end_time": "2024-03-20T02:38:26.288954Z", + "start_time": "2024-03-20T02:38:26.284217Z" + } + }, "outputs": [ { "data": { - "text/plain": [ - "52" - ] + "text/plain": "52" }, - "execution_count": 3, + "execution_count": 4, "metadata": {}, "output_type": "execute_result" } @@ -94,16 +105,19 @@ }, { "cell_type": "code", - "execution_count": 4, - "metadata": {}, + "execution_count": 5, + "metadata": { + "ExecuteTime": { + "end_time": "2024-03-20T02:38:26.298898Z", + "start_time": "2024-03-20T02:38:26.294116Z" + } + }, "outputs": [ { "data": { - "text/plain": [ - "Card(rank='2', suit='spades')" - ] + "text/plain": "Card(rank='2', suit='spades')" }, - "execution_count": 4, + "execution_count": 5, "metadata": {}, "output_type": "execute_result" } @@ -114,16 +128,19 @@ }, { "cell_type": "code", - "execution_count": 5, - "metadata": {}, + "execution_count": 6, + "metadata": { + "ExecuteTime": { + "end_time": "2024-03-20T02:38:26.318592Z", + "start_time": "2024-03-20T02:38:26.314633Z" + } + }, "outputs": [ { "data": { - "text/plain": [ - "Card(rank='A', suit='hearts')" - ] + "text/plain": "Card(rank='A', suit='hearts')" }, - "execution_count": 5, + "execution_count": 6, "metadata": {}, "output_type": "execute_result" } @@ -134,16 +151,19 @@ }, { "cell_type": "code", - "execution_count": 6, - "metadata": {}, + "execution_count": 7, + "metadata": { + "ExecuteTime": { + "end_time": "2024-03-20T02:38:26.333356Z", + "start_time": "2024-03-20T02:38:26.329690Z" + } + }, "outputs": [ { "data": { - "text/plain": [ - "Card(rank='6', suit='diamonds')" - ] + "text/plain": "Card(rank='Q', suit='hearts')" }, - "execution_count": 6, + "execution_count": 7, "metadata": {}, "output_type": "execute_result" } @@ -157,18 +177,19 @@ }, { "cell_type": "code", - "execution_count": 7, - "metadata": {}, + "execution_count": 8, + "metadata": { + "ExecuteTime": { + "end_time": "2024-03-20T02:38:26.359433Z", + "start_time": "2024-03-20T02:38:26.355287Z" + } + }, "outputs": [ { "data": { - "text/plain": [ - "[Card(rank='2', suit='spades'),\n", - " Card(rank='3', suit='spades'),\n", - " Card(rank='4', suit='spades')]" - ] + "text/plain": "[Card(rank='2', suit='spades'),\n Card(rank='3', suit='spades'),\n Card(rank='4', suit='spades')]" }, - "execution_count": 7, + "execution_count": 8, "metadata": {}, "output_type": "execute_result" } @@ -179,19 +200,19 @@ }, { "cell_type": "code", - "execution_count": 8, - "metadata": {}, + "execution_count": 9, + "metadata": { + "ExecuteTime": { + "end_time": "2024-03-20T02:38:26.440400Z", + "start_time": "2024-03-20T02:38:26.435889Z" + } + }, "outputs": [ { "data": { - "text/plain": [ - "[Card(rank='A', suit='spades'),\n", - " Card(rank='A', suit='diamonds'),\n", - " Card(rank='A', suit='clubs'),\n", - " Card(rank='A', suit='hearts')]" - ] + "text/plain": "[Card(rank='A', suit='spades'),\n Card(rank='A', suit='diamonds'),\n Card(rank='A', suit='clubs'),\n Card(rank='A', suit='hearts')]" }, - "execution_count": 8, + "execution_count": 9, "metadata": {}, "output_type": "execute_result" } @@ -202,8 +223,13 @@ }, { "cell_type": "code", - "execution_count": 9, - "metadata": {}, + "execution_count": 10, + "metadata": { + "ExecuteTime": { + "end_time": "2024-03-20T02:38:26.481328Z", + "start_time": "2024-03-20T02:38:26.476414Z" + } + }, "outputs": [ { "name": "stdout", @@ -271,8 +297,13 @@ }, { "cell_type": "code", - "execution_count": 10, - "metadata": {}, + "execution_count": 11, + "metadata": { + "ExecuteTime": { + "end_time": "2024-03-20T02:38:26.546787Z", + "start_time": "2024-03-20T02:38:26.542048Z" + } + }, "outputs": [ { "name": "stdout", @@ -340,16 +371,19 @@ }, { "cell_type": "code", - "execution_count": 11, - "metadata": {}, + "execution_count": 12, + "metadata": { + "ExecuteTime": { + "end_time": "2024-03-20T02:38:26.586583Z", + "start_time": "2024-03-20T02:38:26.581790Z" + } + }, "outputs": [ { "data": { - "text/plain": [ - "True" - ] + "text/plain": "True" }, - "execution_count": 11, + "execution_count": 12, "metadata": {}, "output_type": "execute_result" } @@ -360,16 +394,19 @@ }, { "cell_type": "code", - "execution_count": 12, - "metadata": {}, + "execution_count": 13, + "metadata": { + "ExecuteTime": { + "end_time": "2024-03-20T02:38:26.653553Z", + "start_time": "2024-03-20T02:38:26.648439Z" + } + }, "outputs": [ { "data": { - "text/plain": [ - "False" - ] + "text/plain": "False" }, - "execution_count": 12, + "execution_count": 13, "metadata": {}, "output_type": "execute_result" } @@ -380,8 +417,13 @@ }, { "cell_type": "code", - "execution_count": 13, - "metadata": {}, + "execution_count": 14, + "metadata": { + "ExecuteTime": { + "end_time": "2024-03-20T02:38:26.698346Z", + "start_time": "2024-03-20T02:38:26.693620Z" + } + }, "outputs": [ { "name": "stdout", @@ -449,6 +491,7 @@ " rank_value = FrenchDeck.ranks.index(card.rank)\n", " return rank_value * len(suit_values) + suit_values[card.suit]\n", "\n", + "\n", "for card in sorted(deck, key=spades_high):\n", " print(card)" ] @@ -469,8 +512,13 @@ }, { "cell_type": "code", - "execution_count": 14, - "metadata": {}, + "execution_count": 15, + "metadata": { + "ExecuteTime": { + "end_time": "2024-03-20T02:38:26.774612Z", + "start_time": "2024-03-20T02:38:26.769673Z" + } + }, "outputs": [], "source": [ "import math\n", @@ -501,16 +549,19 @@ }, { "cell_type": "code", - "execution_count": 15, - "metadata": {}, + "execution_count": 16, + "metadata": { + "ExecuteTime": { + "end_time": "2024-03-20T02:38:26.791100Z", + "start_time": "2024-03-20T02:38:26.786481Z" + } + }, "outputs": [ { "data": { - "text/plain": [ - "Vector(4, 5)" - ] + "text/plain": "Vector(4, 5)" }, - "execution_count": 15, + "execution_count": 16, "metadata": {}, "output_type": "execute_result" } @@ -523,16 +574,19 @@ }, { "cell_type": "code", - "execution_count": 16, - "metadata": {}, + "execution_count": 17, + "metadata": { + "ExecuteTime": { + "end_time": "2024-03-20T02:38:26.805307Z", + "start_time": "2024-03-20T02:38:26.800739Z" + } + }, "outputs": [ { "data": { - "text/plain": [ - "5.0" - ] + "text/plain": "5.0" }, - "execution_count": 16, + "execution_count": 17, "metadata": {}, "output_type": "execute_result" } @@ -544,16 +598,19 @@ }, { "cell_type": "code", - "execution_count": 17, - "metadata": {}, + "execution_count": 18, + "metadata": { + "ExecuteTime": { + "end_time": "2024-03-20T02:38:26.872730Z", + "start_time": "2024-03-20T02:38:26.868140Z" + } + }, "outputs": [ { "data": { - "text/plain": [ - "Vector(9, 12)" - ] + "text/plain": "Vector(9, 12)" }, - "execution_count": 17, + "execution_count": 18, "metadata": {}, "output_type": "execute_result" } @@ -564,16 +621,19 @@ }, { "cell_type": "code", - "execution_count": 18, - "metadata": {}, + "execution_count": 19, + "metadata": { + "ExecuteTime": { + "end_time": "2024-03-20T02:38:26.896630Z", + "start_time": "2024-03-20T02:38:26.892290Z" + } + }, "outputs": [ { "data": { - "text/plain": [ - "15.0" - ] + "text/plain": "15.0" }, - "execution_count": 18, + "execution_count": 19, "metadata": {}, "output_type": "execute_result" } @@ -581,6 +641,17 @@ "source": [ "abs(v * 3)" ] + }, + { + "cell_type": "markdown", + "source": [ + "## 1.3.3 自定义类型的布尔值\n", + "\n", + "用户定义的类都是**真值**,除非定义类定义了 `__bool__()` 或者 `__len__()`方法 " + ], + "metadata": { + "collapsed": false + } } ], "metadata": { diff --git a/05-data-classes/test.py b/05-data-classes/test.py new file mode 100644 index 0000000..68ed5bb --- /dev/null +++ b/05-data-classes/test.py @@ -0,0 +1,11 @@ + + +a = [1, 2, 3] +b = a[:] +print(a == b) +print(a is b) + +b[1] = 5 + +print(a) +print(b) \ No newline at end of file