From c2261a2ad6d43a4295bb14d7db8a7d89ff9502cc Mon Sep 17 00:00:00 2001 From: bengbengtu Date: Sat, 5 Mar 2016 20:26:19 +0800 Subject: [PATCH] Revert "auto pull magedu/python2016" --- 002/dict.ipynb | 658 ---- ...350\241\250\350\247\243\346\236\220.ipynb" | 578 ---- 003/test.json | 3 - ...345\255\227\347\254\246\344\270\262.ipynb" | 2877 ----------------- "004/\345\207\275\346\225\260.ipynb" | 2415 -------------- ...350\243\205\351\245\260\345\231\250.ipynb" | 2785 ---------------- notes/20160221.adoc | 2 +- notes/20160227-1.adoc | 25 - notes/20160227-2.adoc | 43 - notes/20160228.adoc | 17 - ...\345\220\221\345\257\271\350\261\241.adoc" | 370 --- 11 files changed, 1 insertion(+), 9772 deletions(-) delete mode 100644 002/dict.ipynb delete mode 100644 "002/\345\210\227\350\241\250\350\247\243\346\236\220.ipynb" delete mode 100644 003/test.json delete mode 100644 "003/\345\255\227\347\254\246\344\270\262.ipynb" delete mode 100644 "004/\345\207\275\346\225\260.ipynb" delete mode 100644 "004/\350\243\205\351\245\260\345\231\250.ipynb" delete mode 100644 notes/20160227-1.adoc delete mode 100644 notes/20160227-2.adoc delete mode 100644 notes/20160228.adoc delete mode 100644 "presentations/005_\351\235\242\345\220\221\345\257\271\350\261\241.adoc" diff --git a/002/dict.ipynb b/002/dict.ipynb deleted file mode 100644 index 42fe2e0..0000000 --- a/002/dict.ipynb +++ /dev/null @@ -1,658 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# 字典" - ] - }, - { - "cell_type": "code", - "execution_count": 1, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "d = {}\n", - "d = dict()\n", - "d = {\"a\":1, \"b\":2}" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## 获取元素" - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "1" - ] - }, - "execution_count": 2, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "d['a']" - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "1" - ] - }, - "execution_count": 3, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "d.get('a')" - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "ename": "KeyError", - "evalue": "'c'", - "output_type": "error", - "traceback": [ - "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[1;31mKeyError\u001b[0m Traceback (most recent call last)", - "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0md\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m'c'\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", - "\u001b[1;31mKeyError\u001b[0m: 'c'" - ] - } - ], - "source": [ - "d['c']" - ] - }, - { - "cell_type": "code", - "execution_count": 5, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "d.get('c')" - ] - }, - { - "cell_type": "code", - "execution_count": 6, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Help on built-in function get:\n", - "\n", - "get(...) method of builtins.dict instance\n", - " D.get(k[,d]) -> D[k] if k in D, else d. d defaults to None.\n", - "\n" - ] - } - ], - "source": [ - "help(d.get)" - ] - }, - { - "cell_type": "code", - "execution_count": 7, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "'abc'" - ] - }, - "execution_count": 7, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "d.get('c', 'abc')" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## 增加和修改元素" - ] - }, - { - "cell_type": "code", - "execution_count": 8, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "d['a'] = 3" - ] - }, - { - "cell_type": "code", - "execution_count": 9, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "{'a': 3, 'b': 2}" - ] - }, - "execution_count": 9, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "d" - ] - }, - { - "cell_type": "code", - "execution_count": 10, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "d['c'] = 4" - ] - }, - { - "cell_type": "code", - "execution_count": 11, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "{'a': 3, 'b': 2, 'c': 4}" - ] - }, - "execution_count": 11, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "d" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## 删除元素" - ] - }, - { - "cell_type": "code", - "execution_count": 12, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "4" - ] - }, - "execution_count": 12, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "d.pop('c')" - ] - }, - { - "cell_type": "code", - "execution_count": 13, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "{'a': 3, 'b': 2}" - ] - }, - "execution_count": 13, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "d" - ] - }, - { - "cell_type": "code", - "execution_count": 14, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "ename": "KeyError", - "evalue": "'c'", - "output_type": "error", - "traceback": [ - "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[1;31mKeyError\u001b[0m Traceback (most recent call last)", - "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0md\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mpop\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'c'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", - "\u001b[1;31mKeyError\u001b[0m: 'c'" - ] - } - ], - "source": [ - "d.pop('c')" - ] - }, - { - "cell_type": "code", - "execution_count": 15, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Help on built-in function pop:\n", - "\n", - "pop(...) method of builtins.dict instance\n", - " D.pop(k[,d]) -> v, remove specified key and return the corresponding value.\n", - " If key is not found, d is returned if given, otherwise KeyError is raised\n", - "\n" - ] - } - ], - "source": [ - "help(d.pop)" - ] - }, - { - "cell_type": "code", - "execution_count": 16, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "'abc'" - ] - }, - "execution_count": 16, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "d.pop('c', 'abc')" - ] - }, - { - "cell_type": "code", - "execution_count": 17, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "('b', 2)" - ] - }, - "execution_count": 17, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "d.popitem()" - ] - }, - { - "cell_type": "code", - "execution_count": 18, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "del d['a']" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## 遍历" - ] - }, - { - "cell_type": "code", - "execution_count": 19, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "d = {'a': 1, 'b': 2, 'c': 3}" - ] - }, - { - "cell_type": "code", - "execution_count": 20, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "dict_keys(['b', 'c', 'a'])" - ] - }, - "execution_count": 20, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "d.keys()" - ] - }, - { - "cell_type": "code", - "execution_count": 21, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "b\n", - "c\n", - "a\n" - ] - } - ], - "source": [ - "for k in d.keys():\n", - " print(k)" - ] - }, - { - "cell_type": "code", - "execution_count": 22, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "dict_values([2, 3, 1])" - ] - }, - "execution_count": 22, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "d.values()" - ] - }, - { - "cell_type": "code", - "execution_count": 23, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "2\n", - "3\n", - "1\n" - ] - } - ], - "source": [ - "for v in d.values():\n", - " print(v)" - ] - }, - { - "cell_type": "code", - "execution_count": 24, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "dict_items([('b', 2), ('c', 3), ('a', 1)])" - ] - }, - "execution_count": 24, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "d.items()" - ] - }, - { - "cell_type": "code", - "execution_count": 26, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "('b', 2)\n", - "('c', 3)\n", - "('a', 1)\n" - ] - } - ], - "source": [ - "for t in d.items():\n", - " print(t)" - ] - }, - { - "cell_type": "code", - "execution_count": 27, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "b => 2\n", - "c => 3\n", - "a => 1\n" - ] - } - ], - "source": [ - "for k, v in d.items():\n", - " print('{0} => {1}'.format(k, v))" - ] - }, - { - "cell_type": "code", - "execution_count": 28, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "d = {}" - ] - }, - { - "cell_type": "code", - "execution_count": 29, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "d['root'] = 'root'" - ] - }, - { - "cell_type": "code", - "execution_count": 30, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "d['root'] = {}" - ] - }, - { - "cell_type": "code", - "execution_count": 31, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "d['root']['left'] = 'left'\n", - "d['root']['right'] = 'right'" - ] - }, - { - "cell_type": "code", - "execution_count": 32, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "{'root': {'left': 'left', 'right': 'right'}}" - ] - }, - "execution_count": 32, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "d" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "collapsed": true - }, - "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.4.2" - } - }, - "nbformat": 4, - "nbformat_minor": 0 -} diff --git "a/002/\345\210\227\350\241\250\350\247\243\346\236\220.ipynb" "b/002/\345\210\227\350\241\250\350\247\243\346\236\220.ipynb" deleted file mode 100644 index 9ec56d0..0000000 --- "a/002/\345\210\227\350\241\250\350\247\243\346\236\220.ipynb" +++ /dev/null @@ -1,578 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# 列表解析" - ] - }, - { - "cell_type": "code", - "execution_count": 1, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "lst = list(range(10))" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## 基本语法" - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]" - ] - }, - "execution_count": 2, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "[x +1 for x in lst]" - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]" - ] - }, - "execution_count": 3, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "l = []\n", - "for x in lst:\n", - " l.append(x+1)\n", - "l" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## 带条件" - ] - }, - { - "cell_type": "code", - "execution_count": 11, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "[1, 3, 5, 7, 9]" - ] - }, - "execution_count": 11, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "[x+1 for x in lst if x % 2 == 0]" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [] - }, - { - "cell_type": "code", - "execution_count": 13, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "[1, 3, 5, 7, 9]" - ] - }, - "execution_count": 13, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "l = []\n", - "for x in lst:\n", - " if x % 2 == 0:\n", - " l.append(x+1)\n", - "l" - ] - }, - { - "cell_type": "code", - "execution_count": 14, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "[5, 7, 9]" - ] - }, - "execution_count": 14, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "[x + 1 for x in lst if x % 2 == 0 if x > 2]" - ] - }, - { - "cell_type": "code", - "execution_count": 17, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "[5, 7, 9]" - ] - }, - "execution_count": 17, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "l = []\n", - "for x in lst:\n", - " if x % 2 == 0 \n", - " if x > 2:\n", - " l.append(x+1)\n", - "l" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## 多个列表" - ] - }, - { - "cell_type": "code", - "execution_count": 18, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "l1 = [1, 3, 5, 7, 9]\n", - "l2 = [0, 2, 4, 6, 8]" - ] - }, - { - "cell_type": "code", - "execution_count": 19, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "[(1, 0),\n", - " (1, 2),\n", - " (1, 4),\n", - " (1, 6),\n", - " (1, 8),\n", - " (3, 0),\n", - " (3, 2),\n", - " (3, 4),\n", - " (3, 6),\n", - " (3, 8),\n", - " (5, 0),\n", - " (5, 2),\n", - " (5, 4),\n", - " (5, 6),\n", - " (5, 8),\n", - " (7, 0),\n", - " (7, 2),\n", - " (7, 4),\n", - " (7, 6),\n", - " (7, 8),\n", - " (9, 0),\n", - " (9, 2),\n", - " (9, 4),\n", - " (9, 6),\n", - " (9, 8)]" - ] - }, - "execution_count": 19, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "[(x, y) for x in l1 for y in l2]" - ] - }, - { - "cell_type": "code", - "execution_count": 20, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "[(1, 0),\n", - " (1, 2),\n", - " (1, 4),\n", - " (1, 6),\n", - " (1, 8),\n", - " (3, 0),\n", - " (3, 2),\n", - " (3, 4),\n", - " (3, 6),\n", - " (3, 8),\n", - " (5, 0),\n", - " (5, 2),\n", - " (5, 4),\n", - " (5, 6),\n", - " (5, 8),\n", - " (7, 0),\n", - " (7, 2),\n", - " (7, 4),\n", - " (7, 6),\n", - " (7, 8),\n", - " (9, 0),\n", - " (9, 2),\n", - " (9, 4),\n", - " (9, 6),\n", - " (9, 8)]" - ] - }, - "execution_count": 20, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "l = []\n", - "for x in l1:\n", - " for y in l2:\n", - " l.append((x, y))\n", - "l" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## 生成集合" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [] - }, - { - "cell_type": "code", - "execution_count": 21, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "s = {1, 3, 5}" - ] - }, - { - "cell_type": "code", - "execution_count": 22, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "{2, 4, 6}" - ] - }, - "execution_count": 22, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "{x +1 for x in s}" - ] - }, - { - "cell_type": "code", - "execution_count": 23, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "[2, 4, 6]" - ] - }, - "execution_count": 23, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "[x+1 for x in s]" - ] - }, - { - "cell_type": "code", - "execution_count": 24, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}" - ] - }, - "execution_count": 24, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "{x+1 for x in range(10)}" - ] - }, - { - "cell_type": "code", - "execution_count": 25, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "{3}" - ] - }, - "execution_count": 25, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "{x+1 for x in [2, 2, 2, 2]}" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## 生成字典" - ] - }, - { - "cell_type": "code", - "execution_count": 26, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "d = {'a':1, 'b': 2}" - ] - }, - { - "cell_type": "code", - "execution_count": 28, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "{'a': 1, 'b': 2}" - ] - }, - "execution_count": 28, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "{k:v for k, v in d.items()}" - ] - }, - { - "cell_type": "code", - "execution_count": 29, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "{1: 8, 3: 8, 5: 8, 7: 8, 9: 8}" - ] - }, - "execution_count": 29, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "{x: y for x in l1 for y in l2}" - ] - }, - { - "cell_type": "code", - "execution_count": 31, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "{'a': 1, 'b': 2}" - ] - }, - "execution_count": 31, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "{k:v for k, v in [('a', 1), ('b', 2)]}" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "collapsed": true - }, - "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.4.2" - } - }, - "nbformat": 4, - "nbformat_minor": 0 -} diff --git a/003/test.json b/003/test.json deleted file mode 100644 index bf6d0b8..0000000 --- a/003/test.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "name": "comyn" -} diff --git "a/003/\345\255\227\347\254\246\344\270\262.ipynb" "b/003/\345\255\227\347\254\246\344\270\262.ipynb" deleted file mode 100644 index 09c5f3f..0000000 --- "a/003/\345\255\227\347\254\246\344\270\262.ipynb" +++ /dev/null @@ -1,2877 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# 字符串与文本操作" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## 字符串是一个线性结构" - ] - }, - { - "cell_type": "code", - "execution_count": 1, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "s = 'i love python'" - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "'i'" - ] - }, - "execution_count": 2, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "s[0]" - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "'n'" - ] - }, - "execution_count": 3, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "s[-1]" - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "'ove p'" - ] - }, - "execution_count": 4, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "s[3:8]" - ] - }, - { - "cell_type": "code", - "execution_count": 5, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "'nohtyp evol i'" - ] - }, - "execution_count": 5, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "s[::-1]" - ] - }, - { - "cell_type": "code", - "execution_count": 6, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "i\n", - " \n", - "l\n", - "o\n", - "v\n", - "e\n", - " \n", - "p\n", - "y\n", - "t\n", - "h\n", - "o\n", - "n\n" - ] - } - ], - "source": [ - "for c in s:\n", - " print(c)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### 字符串是不可变的" - ] - }, - { - "cell_type": "code", - "execution_count": 7, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "' '" - ] - }, - "execution_count": 7, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "s[1]" - ] - }, - { - "cell_type": "code", - "execution_count": 8, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "ename": "TypeError", - "evalue": "'str' object does not support item assignment", - "output_type": "error", - "traceback": [ - "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", - "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0ms\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m]\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;34m'_'\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", - "\u001b[1;31mTypeError\u001b[0m: 'str' object does not support item assignment" - ] - } - ], - "source": [ - "s[1] = '_'" - ] - }, - { - "cell_type": "code", - "execution_count": 9, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "t = (1, 2)" - ] - }, - { - "cell_type": "code", - "execution_count": 10, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "ename": "TypeError", - "evalue": "'tuple' object does not support item assignment", - "output_type": "error", - "traceback": [ - "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", - "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mt\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m]\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;36m3\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", - "\u001b[1;31mTypeError\u001b[0m: 'tuple' object does not support item assignment" - ] - } - ], - "source": [ - "t[0] = 3" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## 字符串的格式化" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### print style format" - ] - }, - { - "cell_type": "code", - "execution_count": 12, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "'l love Python'" - ] - }, - "execution_count": 12, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "'l love %s' % ('Python', )" - ] - }, - { - "cell_type": "code", - "execution_count": 13, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "'I love Python'" - ] - }, - "execution_count": 13, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "'I love %(name)s' % {'name': 'Python'}" - ] - }, - { - "cell_type": "code", - "execution_count": 14, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "'I love Python, Python is my first lang'" - ] - }, - "execution_count": 14, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "'I love %(name)s, %(name)s is my first lang' % {'name': 'Python'}" - ] - }, - { - "cell_type": "code", - "execution_count": 15, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "'I love Python, Python is my first lang'" - ] - }, - "execution_count": 15, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "'I love %s, %s is my first lang' % ('Python', 'Python')" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "#### Conversion" - ] - }, - { - "cell_type": "code", - "execution_count": 16, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "'4'" - ] - }, - "execution_count": 16, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "'%d' % 4" - ] - }, - { - "cell_type": "code", - "execution_count": 17, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "ename": "TypeError", - "evalue": "%d format: a number is required, not str", - "output_type": "error", - "traceback": [ - "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", - "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[1;34m'%d'\u001b[0m \u001b[1;33m%\u001b[0m \u001b[1;34m'str'\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", - "\u001b[1;31mTypeError\u001b[0m: %d format: a number is required, not str" - ] - } - ], - "source": [ - "'%d' % 'str'" - ] - }, - { - "cell_type": "code", - "execution_count": 18, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "'3'" - ] - }, - "execution_count": 18, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "'%d' % 3.4" - ] - }, - { - "cell_type": "code", - "execution_count": 19, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "'1.000000E-09'" - ] - }, - "execution_count": 19, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "'%E' % 0.000000001" - ] - }, - { - "cell_type": "code", - "execution_count": 20, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "'1.000000E+26'" - ] - }, - "execution_count": 20, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "'%E' % 100000000000000000000000000" - ] - }, - { - "cell_type": "code", - "execution_count": 21, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "'0.001'" - ] - }, - "execution_count": 21, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "'%g' % 0.001" - ] - }, - { - "cell_type": "code", - "execution_count": 22, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "'1e-10'" - ] - }, - "execution_count": 22, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "'%g' % 0.0000000001" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "#### flags" - ] - }, - { - "cell_type": "code", - "execution_count": 23, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "' 1'" - ] - }, - "execution_count": 23, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "'%10d' % 1" - ] - }, - { - "cell_type": "code", - "execution_count": 24, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "'0000000001'" - ] - }, - "execution_count": 24, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "'%010d' % 1" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### format方法" - ] - }, - { - "cell_type": "code", - "execution_count": 26, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "'hello, world'" - ] - }, - "execution_count": 26, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "'{0}, {name}'.format('hello', name='world')" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## 字符串的常用操作" - ] - }, - { - "cell_type": "code", - "execution_count": 27, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Help on class str in module builtins:\n", - "\n", - "class str(object)\n", - " | str(object='') -> str\n", - " | str(bytes_or_buffer[, encoding[, errors]]) -> str\n", - " | \n", - " | Create a new string object from the given object. If encoding or\n", - " | errors is specified, then the object must expose a data buffer\n", - " | that will be decoded using the given encoding and error handler.\n", - " | Otherwise, returns the result of object.__str__() (if defined)\n", - " | or repr(object).\n", - " | encoding defaults to sys.getdefaultencoding().\n", - " | errors defaults to 'strict'.\n", - " | \n", - " | Methods defined here:\n", - " | \n", - " | __add__(self, value, /)\n", - " | Return self+value.\n", - " | \n", - " | __contains__(self, key, /)\n", - " | Return key in self.\n", - " | \n", - " | __eq__(self, value, /)\n", - " | Return self==value.\n", - " | \n", - " | __format__(...)\n", - " | S.__format__(format_spec) -> str\n", - " | \n", - " | Return a formatted version of S as described by format_spec.\n", - " | \n", - " | __ge__(self, value, /)\n", - " | Return self>=value.\n", - " | \n", - " | __getattribute__(self, name, /)\n", - " | Return getattr(self, name).\n", - " | \n", - " | __getitem__(self, key, /)\n", - " | Return self[key].\n", - " | \n", - " | __getnewargs__(...)\n", - " | \n", - " | __gt__(self, value, /)\n", - " | Return self>value.\n", - " | \n", - " | __hash__(self, /)\n", - " | Return hash(self).\n", - " | \n", - " | __iter__(self, /)\n", - " | Implement iter(self).\n", - " | \n", - " | __le__(self, value, /)\n", - " | Return self<=value.\n", - " | \n", - " | __len__(self, /)\n", - " | Return len(self).\n", - " | \n", - " | __lt__(self, value, /)\n", - " | Return self size of S in memory, in bytes\n", - " | \n", - " | __str__(self, /)\n", - " | Return str(self).\n", - " | \n", - " | capitalize(...)\n", - " | S.capitalize() -> str\n", - " | \n", - " | Return a capitalized version of S, i.e. make the first character\n", - " | have upper case and the rest lower case.\n", - " | \n", - " | casefold(...)\n", - " | S.casefold() -> str\n", - " | \n", - " | Return a version of S suitable for caseless comparisons.\n", - " | \n", - " | center(...)\n", - " | S.center(width[, fillchar]) -> str\n", - " | \n", - " | Return S centered in a string of length width. Padding is\n", - " | done using the specified fill character (default is a space)\n", - " | \n", - " | count(...)\n", - " | S.count(sub[, start[, end]]) -> int\n", - " | \n", - " | Return the number of non-overlapping occurrences of substring sub in\n", - " | string S[start:end]. Optional arguments start and end are\n", - " | interpreted as in slice notation.\n", - " | \n", - " | encode(...)\n", - " | S.encode(encoding='utf-8', errors='strict') -> bytes\n", - " | \n", - " | Encode S using the codec registered for encoding. Default encoding\n", - " | is 'utf-8'. errors may be given to set a different error\n", - " | handling scheme. Default is 'strict' meaning that encoding errors raise\n", - " | a UnicodeEncodeError. Other possible values are 'ignore', 'replace' and\n", - " | 'xmlcharrefreplace' as well as any other name registered with\n", - " | codecs.register_error that can handle UnicodeEncodeErrors.\n", - " | \n", - " | endswith(...)\n", - " | S.endswith(suffix[, start[, end]]) -> bool\n", - " | \n", - " | Return True if S ends with the specified suffix, False otherwise.\n", - " | With optional start, test S beginning at that position.\n", - " | With optional end, stop comparing S at that position.\n", - " | suffix can also be a tuple of strings to try.\n", - " | \n", - " | expandtabs(...)\n", - " | S.expandtabs(tabsize=8) -> str\n", - " | \n", - " | Return a copy of S where all tab characters are expanded using spaces.\n", - " | If tabsize is not given, a tab size of 8 characters is assumed.\n", - " | \n", - " | find(...)\n", - " | S.find(sub[, start[, end]]) -> int\n", - " | \n", - " | Return the lowest index in S where substring sub is found,\n", - " | such that sub is contained within S[start:end]. Optional\n", - " | arguments start and end are interpreted as in slice notation.\n", - " | \n", - " | Return -1 on failure.\n", - " | \n", - " | format(...)\n", - " | S.format(*args, **kwargs) -> str\n", - " | \n", - " | Return a formatted version of S, using substitutions from args and kwargs.\n", - " | The substitutions are identified by braces ('{' and '}').\n", - " | \n", - " | format_map(...)\n", - " | S.format_map(mapping) -> str\n", - " | \n", - " | Return a formatted version of S, using substitutions from mapping.\n", - " | The substitutions are identified by braces ('{' and '}').\n", - " | \n", - " | index(...)\n", - " | S.index(sub[, start[, end]]) -> int\n", - " | \n", - " | Like S.find() but raise ValueError when the substring is not found.\n", - " | \n", - " | isalnum(...)\n", - " | S.isalnum() -> bool\n", - " | \n", - " | Return True if all characters in S are alphanumeric\n", - " | and there is at least one character in S, False otherwise.\n", - " | \n", - " | isalpha(...)\n", - " | S.isalpha() -> bool\n", - " | \n", - " | Return True if all characters in S are alphabetic\n", - " | and there is at least one character in S, False otherwise.\n", - " | \n", - " | isdecimal(...)\n", - " | S.isdecimal() -> bool\n", - " | \n", - " | Return True if there are only decimal characters in S,\n", - " | False otherwise.\n", - " | \n", - " | isdigit(...)\n", - " | S.isdigit() -> bool\n", - " | \n", - " | Return True if all characters in S are digits\n", - " | and there is at least one character in S, False otherwise.\n", - " | \n", - " | isidentifier(...)\n", - " | S.isidentifier() -> bool\n", - " | \n", - " | Return True if S is a valid identifier according\n", - " | to the language definition.\n", - " | \n", - " | Use keyword.iskeyword() to test for reserved identifiers\n", - " | such as \"def\" and \"class\".\n", - " | \n", - " | islower(...)\n", - " | S.islower() -> bool\n", - " | \n", - " | Return True if all cased characters in S are lowercase and there is\n", - " | at least one cased character in S, False otherwise.\n", - " | \n", - " | isnumeric(...)\n", - " | S.isnumeric() -> bool\n", - " | \n", - " | Return True if there are only numeric characters in S,\n", - " | False otherwise.\n", - " | \n", - " | isprintable(...)\n", - " | S.isprintable() -> bool\n", - " | \n", - " | Return True if all characters in S are considered\n", - " | printable in repr() or S is empty, False otherwise.\n", - " | \n", - " | isspace(...)\n", - " | S.isspace() -> bool\n", - " | \n", - " | Return True if all characters in S are whitespace\n", - " | and there is at least one character in S, False otherwise.\n", - " | \n", - " | istitle(...)\n", - " | S.istitle() -> bool\n", - " | \n", - " | Return True if S is a titlecased string and there is at least one\n", - " | character in S, i.e. upper- and titlecase characters may only\n", - " | follow uncased characters and lowercase characters only cased ones.\n", - " | Return False otherwise.\n", - " | \n", - " | isupper(...)\n", - " | S.isupper() -> bool\n", - " | \n", - " | Return True if all cased characters in S are uppercase and there is\n", - " | at least one cased character in S, False otherwise.\n", - " | \n", - " | join(...)\n", - " | S.join(iterable) -> str\n", - " | \n", - " | Return a string which is the concatenation of the strings in the\n", - " | iterable. The separator between elements is S.\n", - " | \n", - " | ljust(...)\n", - " | S.ljust(width[, fillchar]) -> str\n", - " | \n", - " | Return S left-justified in a Unicode string of length width. Padding is\n", - " | done using the specified fill character (default is a space).\n", - " | \n", - " | lower(...)\n", - " | S.lower() -> str\n", - " | \n", - " | Return a copy of the string S converted to lowercase.\n", - " | \n", - " | lstrip(...)\n", - " | S.lstrip([chars]) -> str\n", - " | \n", - " | Return a copy of the string S with leading whitespace removed.\n", - " | If chars is given and not None, remove characters in chars instead.\n", - " | \n", - " | partition(...)\n", - " | S.partition(sep) -> (head, sep, tail)\n", - " | \n", - " | Search for the separator sep in S, and return the part before it,\n", - " | the separator itself, and the part after it. If the separator is not\n", - " | found, return S and two empty strings.\n", - " | \n", - " | replace(...)\n", - " | S.replace(old, new[, count]) -> str\n", - " | \n", - " | Return a copy of S with all occurrences of substring\n", - " | old replaced by new. If the optional argument count is\n", - " | given, only the first count occurrences are replaced.\n", - " | \n", - " | rfind(...)\n", - " | S.rfind(sub[, start[, end]]) -> int\n", - " | \n", - " | Return the highest index in S where substring sub is found,\n", - " | such that sub is contained within S[start:end]. Optional\n", - " | arguments start and end are interpreted as in slice notation.\n", - " | \n", - " | Return -1 on failure.\n", - " | \n", - " | rindex(...)\n", - " | S.rindex(sub[, start[, end]]) -> int\n", - " | \n", - " | Like S.rfind() but raise ValueError when the substring is not found.\n", - " | \n", - " | rjust(...)\n", - " | S.rjust(width[, fillchar]) -> str\n", - " | \n", - " | Return S right-justified in a string of length width. Padding is\n", - " | done using the specified fill character (default is a space).\n", - " | \n", - " | rpartition(...)\n", - " | S.rpartition(sep) -> (head, sep, tail)\n", - " | \n", - " | Search for the separator sep in S, starting at the end of S, and return\n", - " | the part before it, the separator itself, and the part after it. If the\n", - " | separator is not found, return two empty strings and S.\n", - " | \n", - " | rsplit(...)\n", - " | S.rsplit(sep=None, maxsplit=-1) -> list of strings\n", - " | \n", - " | Return a list of the words in S, using sep as the\n", - " | delimiter string, starting at the end of the string and\n", - " | working to the front. If maxsplit is given, at most maxsplit\n", - " | splits are done. If sep is not specified, any whitespace string\n", - " | is a separator.\n", - " | \n", - " | rstrip(...)\n", - " | S.rstrip([chars]) -> str\n", - " | \n", - " | Return a copy of the string S with trailing whitespace removed.\n", - " | If chars is given and not None, remove characters in chars instead.\n", - " | \n", - " | split(...)\n", - " | S.split(sep=None, maxsplit=-1) -> list of strings\n", - " | \n", - " | Return a list of the words in S, using sep as the\n", - " | delimiter string. If maxsplit is given, at most maxsplit\n", - " | splits are done. If sep is not specified or is None, any\n", - " | whitespace string is a separator and empty strings are\n", - " | removed from the result.\n", - " | \n", - " | splitlines(...)\n", - " | S.splitlines([keepends]) -> list of strings\n", - " | \n", - " | Return a list of the lines in S, breaking at line boundaries.\n", - " | Line breaks are not included in the resulting list unless keepends\n", - " | is given and true.\n", - " | \n", - " | startswith(...)\n", - " | S.startswith(prefix[, start[, end]]) -> bool\n", - " | \n", - " | Return True if S starts with the specified prefix, False otherwise.\n", - " | With optional start, test S beginning at that position.\n", - " | With optional end, stop comparing S at that position.\n", - " | prefix can also be a tuple of strings to try.\n", - " | \n", - " | strip(...)\n", - " | S.strip([chars]) -> str\n", - " | \n", - " | Return a copy of the string S with leading and trailing\n", - " | whitespace removed.\n", - " | If chars is given and not None, remove characters in chars instead.\n", - " | \n", - " | swapcase(...)\n", - " | S.swapcase() -> str\n", - " | \n", - " | Return a copy of S with uppercase characters converted to lowercase\n", - " | and vice versa.\n", - " | \n", - " | title(...)\n", - " | S.title() -> str\n", - " | \n", - " | Return a titlecased version of S, i.e. words start with title case\n", - " | characters, all remaining cased characters have lower case.\n", - " | \n", - " | translate(...)\n", - " | S.translate(table) -> str\n", - " | \n", - " | Return a copy of the string S, where all characters have been mapped\n", - " | through the given translation table, which must be a mapping of\n", - " | Unicode ordinals to Unicode ordinals, strings, or None.\n", - " | Unmapped characters are left untouched. Characters mapped to None\n", - " | are deleted.\n", - " | \n", - " | upper(...)\n", - " | S.upper() -> str\n", - " | \n", - " | Return a copy of S converted to uppercase.\n", - " | \n", - " | zfill(...)\n", - " | S.zfill(width) -> str\n", - " | \n", - " | Pad a numeric string S with zeros on the left, to fill a field\n", - " | of the specified width. The string S is never truncated.\n", - " | \n", - " | ----------------------------------------------------------------------\n", - " | Static methods defined here:\n", - " | \n", - " | maketrans(x, y=None, z=None, /)\n", - " | Return a translation table usable for str.translate().\n", - " | \n", - " | If there is only one argument, it must be a dictionary mapping Unicode\n", - " | ordinals (integers) or characters to Unicode ordinals, strings or None.\n", - " | Character keys will be then converted to ordinals.\n", - " | If there are two arguments, they must be strings of equal length, and\n", - " | in the resulting dictionary, each character in x will be mapped to the\n", - " | character at the same position in y. If there is a third argument, it\n", - " | must be a string, whose characters will be mapped to None in the result.\n", - "\n" - ] - } - ], - "source": [ - "help(str)" - ] - }, - { - "cell_type": "code", - "execution_count": 28, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "'I love Python'" - ] - }, - "execution_count": 28, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "lst = ['I', 'love', 'Python']\n", - "\n", - "' '.join(lst)" - ] - }, - { - "cell_type": "code", - "execution_count": 29, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "'I_love_Python'" - ] - }, - "execution_count": 29, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "'_'.join(lst)" - ] - }, - { - "cell_type": "code", - "execution_count": 30, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "'I love Python '" - ] - }, - "execution_count": 30, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "ret = ''\n", - "for s in lst:\n", - " ret += s\n", - " ret += ' '\n", - "ret" - ] - }, - { - "cell_type": "code", - "execution_count": 31, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Help on method_descriptor:\n", - "\n", - "split(...)\n", - " S.split(sep=None, maxsplit=-1) -> list of strings\n", - " \n", - " Return a list of the words in S, using sep as the\n", - " delimiter string. If maxsplit is given, at most maxsplit\n", - " splits are done. If sep is not specified or is None, any\n", - " whitespace string is a separator and empty strings are\n", - " removed from the result.\n", - "\n" - ] - } - ], - "source": [ - "help(str.split)" - ] - }, - { - "cell_type": "code", - "execution_count": 32, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Help on method_descriptor:\n", - "\n", - "rsplit(...)\n", - " S.rsplit(sep=None, maxsplit=-1) -> list of strings\n", - " \n", - " Return a list of the words in S, using sep as the\n", - " delimiter string, starting at the end of the string and\n", - " working to the front. If maxsplit is given, at most maxsplit\n", - " splits are done. If sep is not specified, any whitespace string\n", - " is a separator.\n", - "\n" - ] - } - ], - "source": [ - "help(str.rsplit)" - ] - }, - { - "cell_type": "code", - "execution_count": 33, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "'Python'" - ] - }, - "execution_count": 33, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "s" - ] - }, - { - "cell_type": "code", - "execution_count": 34, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "s = 'I love Python'" - ] - }, - { - "cell_type": "code", - "execution_count": 35, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "['I', 'love', 'Python']" - ] - }, - "execution_count": 35, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "s.split()" - ] - }, - { - "cell_type": "code", - "execution_count": 36, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "['I l', 've Pyth', 'n']" - ] - }, - "execution_count": 36, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "s.split('o')" - ] - }, - { - "cell_type": "code", - "execution_count": 37, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "['I', 'love Python']" - ] - }, - "execution_count": 37, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "s.split(' ', 1)" - ] - }, - { - "cell_type": "code", - "execution_count": 39, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "s = 'root:x:0:0:root:/root:/bin/bash'" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [] - }, - { - "cell_type": "code", - "execution_count": 40, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "['root', 'x:0:0:root:/root:/bin/bash']" - ] - }, - "execution_count": 40, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "s.split(':', 1)" - ] - }, - { - "cell_type": "code", - "execution_count": 41, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "username, _ = s.split(':', 1)" - ] - }, - { - "cell_type": "code", - "execution_count": 42, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "'root'" - ] - }, - "execution_count": 42, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "username" - ] - }, - { - "cell_type": "code", - "execution_count": 43, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "s = 'URL:http://www.magedu.com'" - ] - }, - { - "cell_type": "code", - "execution_count": 44, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "key, value = s.split(':', 1)" - ] - }, - { - "cell_type": "code", - "execution_count": 45, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "URL\n", - "http://www.magedu.com\n" - ] - } - ], - "source": [ - "print(key)\n", - "print(value)" - ] - }, - { - "cell_type": "code", - "execution_count": 46, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "['URL', 'http', '//www.magedu.com']" - ] - }, - "execution_count": 46, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "s.split(':')" - ] - }, - { - "cell_type": "code", - "execution_count": 47, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "['URL', 'http', '//www.magedu.com']" - ] - }, - "execution_count": 47, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "s.rsplit(':')" - ] - }, - { - "cell_type": "code", - "execution_count": 48, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "['URL:http', '//www.magedu.com']" - ] - }, - "execution_count": 48, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "s.rsplit(':', 1)" - ] - }, - { - "cell_type": "code", - "execution_count": 49, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Help on built-in function splitlines:\n", - "\n", - "splitlines(...) method of builtins.str instance\n", - " S.splitlines([keepends]) -> list of strings\n", - " \n", - " Return a list of the lines in S, breaking at line boundaries.\n", - " Line breaks are not included in the resulting list unless keepends\n", - " is given and true.\n", - "\n" - ] - } - ], - "source": [ - "help(s.splitlines)" - ] - }, - { - "cell_type": "code", - "execution_count": 50, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "s = '''\n", - "I love Python\n", - "I also love linux\n", - "'''" - ] - }, - { - "cell_type": "code", - "execution_count": 51, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "'\\nI love Python\\nI also love linux\\n'" - ] - }, - "execution_count": 51, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "s" - ] - }, - { - "cell_type": "code", - "execution_count": 52, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "['', 'I love Python', 'I also love linux']" - ] - }, - "execution_count": 52, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "s.splitlines()" - ] - }, - { - "cell_type": "code", - "execution_count": 53, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "['\\n', 'I love Python\\n', 'I also love linux\\n']" - ] - }, - "execution_count": 53, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "s.splitlines(True)" - ] - }, - { - "cell_type": "code", - "execution_count": 54, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "['', 'I love Python', 'I also love linux']" - ] - }, - "execution_count": 54, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "s.splitlines(False)" - ] - }, - { - "cell_type": "code", - "execution_count": 55, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Help on built-in function partition:\n", - "\n", - "partition(...) method of builtins.str instance\n", - " S.partition(sep) -> (head, sep, tail)\n", - " \n", - " Search for the separator sep in S, and return the part before it,\n", - " the separator itself, and the part after it. If the separator is not\n", - " found, return S and two empty strings.\n", - "\n" - ] - } - ], - "source": [ - "help(s.partition)" - ] - }, - { - "cell_type": "code", - "execution_count": 56, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "s = 'root:x:0:0:root:/root:/bin/bash'" - ] - }, - { - "cell_type": "code", - "execution_count": 57, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "'root:x:0:0:root:/root:/bin/bash'" - ] - }, - "execution_count": 57, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "s" - ] - }, - { - "cell_type": "code", - "execution_count": 58, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "('root', ':', 'x:0:0:root:/root:/bin/bash')" - ] - }, - "execution_count": 58, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "s.partition(':')" - ] - }, - { - "cell_type": "code", - "execution_count": 59, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "h, _, t = s.partition(':')" - ] - }, - { - "cell_type": "code", - "execution_count": 60, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "'x:0:0:root:/root:/bin/bash'" - ] - }, - "execution_count": 60, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "t" - ] - }, - { - "cell_type": "code", - "execution_count": 64, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "h, _, t = t.partition(':')" - ] - }, - { - "cell_type": "code", - "execution_count": 65, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "'0:0:root:/root:/bin/bash'" - ] - }, - "execution_count": 65, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "t" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [] - }, - { - "cell_type": "code", - "execution_count": 63, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "'x:0:0:root:/root:/bin/bash'" - ] - }, - "execution_count": 63, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "t" - ] - }, - { - "cell_type": "code", - "execution_count": 66, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "('root:x:0:0:root:/root', ':', '/bin/bash')" - ] - }, - "execution_count": 66, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "s.rpartition(':')" - ] - }, - { - "cell_type": "code", - "execution_count": 67, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "s = 'I love Python'" - ] - }, - { - "cell_type": "code", - "execution_count": 68, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "'I love python'" - ] - }, - "execution_count": 68, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "s.capitalize()" - ] - }, - { - "cell_type": "code", - "execution_count": 69, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "s = 'i love python'" - ] - }, - { - "cell_type": "code", - "execution_count": 70, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "'I love python'" - ] - }, - "execution_count": 70, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "s.capitalize()" - ] - }, - { - "cell_type": "code", - "execution_count": 71, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "'I Love Python'" - ] - }, - "execution_count": 71, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "s.title()" - ] - }, - { - "cell_type": "code", - "execution_count": 72, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "'I LOVE PYTHON'" - ] - }, - "execution_count": 72, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "s.upper()" - ] - }, - { - "cell_type": "code", - "execution_count": 73, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "'i love python'" - ] - }, - "execution_count": 73, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "s.lower()" - ] - }, - { - "cell_type": "code", - "execution_count": 74, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "s = s.title()" - ] - }, - { - "cell_type": "code", - "execution_count": 75, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "'I Love Python'" - ] - }, - "execution_count": 75, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "s" - ] - }, - { - "cell_type": "code", - "execution_count": 76, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "'i lOVE pYTHON'" - ] - }, - "execution_count": 76, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "s.swapcase()" - ] - }, - { - "cell_type": "code", - "execution_count": 77, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Help on built-in function center:\n", - "\n", - "center(...) method of builtins.str instance\n", - " S.center(width[, fillchar]) -> str\n", - " \n", - " Return S centered in a string of length width. Padding is\n", - " done using the specified fill character (default is a space)\n", - "\n" - ] - } - ], - "source": [ - "help(s.center)" - ] - }, - { - "cell_type": "code", - "execution_count": 78, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "s = 'Python'" - ] - }, - { - "cell_type": "code", - "execution_count": 79, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "' Python '" - ] - }, - "execution_count": 79, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "s.center(20)" - ] - }, - { - "cell_type": "code", - "execution_count": 80, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "'*******Python*******'" - ] - }, - "execution_count": 80, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "s.center(20, '*')" - ] - }, - { - "cell_type": "code", - "execution_count": 81, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Help on built-in function ljust:\n", - "\n", - "ljust(...) method of builtins.str instance\n", - " S.ljust(width[, fillchar]) -> str\n", - " \n", - " Return S left-justified in a Unicode string of length width. Padding is\n", - " done using the specified fill character (default is a space).\n", - "\n" - ] - } - ], - "source": [ - "help(s.ljust)" - ] - }, - { - "cell_type": "code", - "execution_count": 82, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "'Python '" - ] - }, - "execution_count": 82, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "s.ljust(20)" - ] - }, - { - "cell_type": "code", - "execution_count": 83, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "'Python**************'" - ] - }, - "execution_count": 83, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "s.ljust(20, '*')" - ] - }, - { - "cell_type": "code", - "execution_count": 84, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "'**************Python'" - ] - }, - "execution_count": 84, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "s.rjust(20, '*')" - ] - }, - { - "cell_type": "code", - "execution_count": 85, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "s = s.center(20)" - ] - }, - { - "cell_type": "code", - "execution_count": 86, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "' Python '" - ] - }, - "execution_count": 86, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "s" - ] - }, - { - "cell_type": "code", - "execution_count": 87, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "'Python'" - ] - }, - "execution_count": 87, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "s.strip()" - ] - }, - { - "cell_type": "code", - "execution_count": 88, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "s = 'abc\\n'" - ] - }, - { - "cell_type": "code", - "execution_count": 89, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "'abc\\n'" - ] - }, - "execution_count": 89, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "s" - ] - }, - { - "cell_type": "code", - "execution_count": 90, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "'abc'" - ] - }, - "execution_count": 90, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "s.strip()" - ] - }, - { - "cell_type": "code", - "execution_count": 91, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "f = open('/etc/passwd', 'r')" - ] - }, - { - "cell_type": "code", - "execution_count": 92, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "'root:x:0:0:root:/root:/bin/bash\\n'" - ] - }, - "execution_count": 92, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "f.readline()" - ] - }, - { - "cell_type": "code", - "execution_count": 93, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "'bin:x:1:1:bin:/bin:/sbin/nologin'" - ] - }, - "execution_count": 93, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "f.readline().strip()" - ] - }, - { - "cell_type": "code", - "execution_count": 94, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "f.close()" - ] - }, - { - "cell_type": "code", - "execution_count": 95, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "f = open('/etc/passwd', 'r')" - ] - }, - { - "cell_type": "code", - "execution_count": 96, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "/bin/bash\n" - ] - } - ], - "source": [ - "for line in f.readlines():\n", - " line = line.strip()\n", - " if line.startswith('root:'):\n", - " _, shell = line.rsplit(':', 1)\n", - " print(shell)\n" - ] - }, - { - "cell_type": "code", - "execution_count": 97, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Help on built-in function startswith:\n", - "\n", - "startswith(...) method of builtins.str instance\n", - " S.startswith(prefix[, start[, end]]) -> bool\n", - " \n", - " Return True if S starts with the specified prefix, False otherwise.\n", - " With optional start, test S beginning at that position.\n", - " With optional end, stop comparing S at that position.\n", - " prefix can also be a tuple of strings to try.\n", - "\n" - ] - } - ], - "source": [ - "help(s.startswith)" - ] - }, - { - "cell_type": "code", - "execution_count": 98, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Help on built-in function endswith:\n", - "\n", - "endswith(...) method of builtins.str instance\n", - " S.endswith(suffix[, start[, end]]) -> bool\n", - " \n", - " Return True if S ends with the specified suffix, False otherwise.\n", - " With optional start, test S beginning at that position.\n", - " With optional end, stop comparing S at that position.\n", - " suffix can also be a tuple of strings to try.\n", - "\n" - ] - } - ], - "source": [ - "help(s.endswith)" - ] - }, - { - "cell_type": "code", - "execution_count": 99, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "'abc\\n'" - ] - }, - "execution_count": 99, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "s" - ] - }, - { - "cell_type": "code", - "execution_count": 100, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "s = 'root:x:0:0:root:/root:/bin/bash\\n'" - ] - }, - { - "cell_type": "code", - "execution_count": 101, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "'root:x:0:0:root:/root:/bin/bash\\n'" - ] - }, - "execution_count": 101, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "s" - ] - }, - { - "cell_type": "code", - "execution_count": 102, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "3" - ] - }, - "execution_count": 102, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "s.count('r')" - ] - }, - { - "cell_type": "code", - "execution_count": 103, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "3" - ] - }, - "execution_count": 103, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "s.count('root')" - ] - }, - { - "cell_type": "code", - "execution_count": 104, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "0" - ] - }, - "execution_count": 104, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "s.index('r')" - ] - }, - { - "cell_type": "code", - "execution_count": 105, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "29" - ] - }, - "execution_count": 105, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "s.index('s')" - ] - }, - { - "cell_type": "code", - "execution_count": 106, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "0" - ] - }, - "execution_count": 106, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "s.index('root')" - ] - }, - { - "cell_type": "code", - "execution_count": 107, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "27" - ] - }, - "execution_count": 107, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "s.index('bash')" - ] - }, - { - "cell_type": "code", - "execution_count": 108, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Help on built-in function find:\n", - "\n", - "find(...) method of builtins.str instance\n", - " S.find(sub[, start[, end]]) -> int\n", - " \n", - " Return the lowest index in S where substring sub is found,\n", - " such that sub is contained within S[start:end]. Optional\n", - " arguments start and end are interpreted as in slice notation.\n", - " \n", - " Return -1 on failure.\n", - "\n" - ] - } - ], - "source": [ - "help(s.find)" - ] - }, - { - "cell_type": "code", - "execution_count": 109, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "0" - ] - }, - "execution_count": 109, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "s.find('root')" - ] - }, - { - "cell_type": "code", - "execution_count": 110, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "11" - ] - }, - "execution_count": 110, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "s.find('root', 3)" - ] - }, - { - "cell_type": "code", - "execution_count": 111, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "-1" - ] - }, - "execution_count": 111, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "s.find('comyn')" - ] - }, - { - "cell_type": "code", - "execution_count": 112, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "ename": "ValueError", - "evalue": "substring not found", - "output_type": "error", - "traceback": [ - "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[1;31mValueError\u001b[0m Traceback (most recent call last)", - "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0ms\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mindex\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'comyn'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", - "\u001b[1;31mValueError\u001b[0m: substring not found" - ] - } - ], - "source": [ - "s.index('comyn')" - ] - }, - { - "cell_type": "code", - "execution_count": 113, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Help on built-in function replace:\n", - "\n", - "replace(...) method of builtins.str instance\n", - " S.replace(old, new[, count]) -> str\n", - " \n", - " Return a copy of S with all occurrences of substring\n", - " old replaced by new. If the optional argument count is\n", - " given, only the first count occurrences are replaced.\n", - "\n" - ] - } - ], - "source": [ - "help(s.replace)" - ] - }, - { - "cell_type": "code", - "execution_count": 114, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "'root:x:0:0:root:/root:/bin/bash\\n'" - ] - }, - "execution_count": 114, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "s" - ] - }, - { - "cell_type": "code", - "execution_count": 115, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "'comyn:x:0:0:comyn:/comyn:/bin/bash\\n'" - ] - }, - "execution_count": 115, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "s.replace('root', 'comyn')" - ] - }, - { - "cell_type": "code", - "execution_count": 116, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "'comyn:x:0:0:root:/root:/bin/bash\\n'" - ] - }, - "execution_count": 116, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "s.replace('root', 'comyn', 1)" - ] - }, - { - "cell_type": "code", - "execution_count": 117, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "'comyn:x:0:0:comyn:/comyn:/bin/bash\\n'" - ] - }, - "execution_count": 117, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "s.replace('root', 'comyn', -1)" - ] - }, - { - "cell_type": "code", - "execution_count": 118, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "'comyn:x:0:0:comyn:/comyn:/bin/bash\\n'" - ] - }, - "execution_count": 118, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "s.replace('root', 'comyn', -2)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "collapsed": true - }, - "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.4.2" - } - }, - "nbformat": 4, - "nbformat_minor": 0 -} diff --git "a/004/\345\207\275\346\225\260.ipynb" "b/004/\345\207\275\346\225\260.ipynb" deleted file mode 100644 index 62d042a..0000000 --- "a/004/\345\207\275\346\225\260.ipynb" +++ /dev/null @@ -1,2415 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# 函数" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## 函数的定义与调用" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### 基本语法" - ] - }, - { - "cell_type": "code", - "execution_count": 1, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "def fn():\n", - " pass" - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "fn()" - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "def fn():\n", - " print('execute fn')" - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "execute fn\n" - ] - } - ], - "source": [ - "fn()" - ] - }, - { - "cell_type": "code", - "execution_count": 5, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "execute fn\n" - ] - } - ], - "source": [ - "a = fn()" - ] - }, - { - "cell_type": "code", - "execution_count": 6, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "a" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## 函数参数" - ] - }, - { - "cell_type": "code", - "execution_count": 7, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "def add(x, y):\n", - " return x + y" - ] - }, - { - "cell_type": "code", - "execution_count": 8, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "3" - ] - }, - "execution_count": 8, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "add(1, 2)" - ] - }, - { - "cell_type": "code", - "execution_count": 9, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "x = 1\n", - "y = 2" - ] - }, - { - "cell_type": "code", - "execution_count": 10, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "def swap(x, y):\n", - " x, y = y, x" - ] - }, - { - "cell_type": "code", - "execution_count": 11, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "swap(x, y)" - ] - }, - { - "cell_type": "code", - "execution_count": 12, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "1" - ] - }, - "execution_count": 12, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "x" - ] - }, - { - "cell_type": "code", - "execution_count": 13, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "2" - ] - }, - "execution_count": 13, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "y" - ] - }, - { - "cell_type": "code", - "execution_count": 14, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "lst = [1, 2, 3]" - ] - }, - { - "cell_type": "code", - "execution_count": 15, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "def append(item):\n", - " lst.append(item)\n", - " " - ] - }, - { - "cell_type": "code", - "execution_count": 16, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "append(4)" - ] - }, - { - "cell_type": "code", - "execution_count": 17, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "[1, 2, 3, 4]" - ] - }, - "execution_count": 17, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "lst" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### 位置参数和关键字参数" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "位置参数和关键字参数是在调用的时候决定的" - ] - }, - { - "cell_type": "code", - "execution_count": 22, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "def add(x, y):\n", - " print(\"x = {0}\".format(x))\n", - " print(\"y = {0}\".format(y))\n", - " return x + y" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "* 位置参数 通过参数传递的位置来决定" - ] - }, - { - "cell_type": "code", - "execution_count": 23, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "x = 1\n", - "y = 2\n" - ] - }, - { - "data": { - "text/plain": [ - "3" - ] - }, - "execution_count": 23, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "add(1, 2)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "* 关键字参数 通过参数名称来决定" - ] - }, - { - "cell_type": "code", - "execution_count": 24, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "x = 1\n", - "y = 2\n" - ] - }, - { - "data": { - "text/plain": [ - "3" - ] - }, - "execution_count": 24, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "add(x=1, y=2)" - ] - }, - { - "cell_type": "code", - "execution_count": 25, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "x = 1\n", - "y = 2\n" - ] - }, - { - "data": { - "text/plain": [ - "3" - ] - }, - "execution_count": 25, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "add(y=2, x=1)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "* 混合使用 关键字参数必须在位置参数之后" - ] - }, - { - "cell_type": "code", - "execution_count": 26, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "x = 1\n", - "y = 2\n" - ] - }, - { - "data": { - "text/plain": [ - "3" - ] - }, - "execution_count": 26, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "add(1, y=2)" - ] - }, - { - "cell_type": "code", - "execution_count": 27, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "ename": "SyntaxError", - "evalue": "non-keyword arg after keyword arg (, line 1)", - "output_type": "error", - "traceback": [ - "\u001b[1;36m File \u001b[1;32m\"\"\u001b[1;36m, line \u001b[1;32m1\u001b[0m\n\u001b[1;33m add(x=1, 2)\u001b[0m\n\u001b[1;37m ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m non-keyword arg after keyword arg\n" - ] - } - ], - "source": [ - "add(x=1, 2)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### 默认参数" - ] - }, - { - "cell_type": "code", - "execution_count": 28, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "def inc(x, i=1):\n", - " return x + i" - ] - }, - { - "cell_type": "code", - "execution_count": 29, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "6" - ] - }, - "execution_count": 29, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "inc(5)" - ] - }, - { - "cell_type": "code", - "execution_count": 31, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "8" - ] - }, - "execution_count": 31, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "inc(5, 3)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "当默认参赛和关键字参数一起使用的时候,世界都是美好的" - ] - }, - { - "cell_type": "code", - "execution_count": 32, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "ename": "SyntaxError", - "evalue": "non-default argument follows default argument (, line 1)", - "output_type": "error", - "traceback": [ - "\u001b[1;36m File \u001b[1;32m\"\"\u001b[1;36m, line \u001b[1;32m1\u001b[0m\n\u001b[1;33m def inc(i=1, x):\u001b[0m\n\u001b[1;37m ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m non-default argument follows default argument\n" - ] - } - ], - "source": [ - "def inc(i=1, x):\n", - " return x + 1" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### 可变参数" - ] - }, - { - "cell_type": "code", - "execution_count": 33, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "def sum(lst):\n", - " ret = 0\n", - " for x in lst:\n", - " ret += x\n", - " return ret" - ] - }, - { - "cell_type": "code", - "execution_count": 34, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "15" - ] - }, - "execution_count": 34, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "sum([1, 2, 3, 4, 5])" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "* 可变位置参数" - ] - }, - { - "cell_type": "code", - "execution_count": 37, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "def sum(*args):\n", - " ret = 0\n", - " print(args)\n", - " for x in args:\n", - " ret += x\n", - " return ret" - ] - }, - { - "cell_type": "code", - "execution_count": 38, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "(1, 2, 3, 4, 5)\n" - ] - }, - { - "data": { - "text/plain": [ - "15" - ] - }, - "execution_count": 38, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "sum(1, 2, 3, 4, 5)" - ] - }, - { - "cell_type": "code", - "execution_count": 39, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "(5, 4, 3, 2, 1)\n" - ] - }, - { - "data": { - "text/plain": [ - "15" - ] - }, - "execution_count": 39, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "sum(5, 4, 3, 2, 1)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "* 可变关键字参数" - ] - }, - { - "cell_type": "code", - "execution_count": 42, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "def print_info(**kwargs):\n", - " for k, v in kwargs.items():\n", - " print('{0} => {1}'.format(k, v))" - ] - }, - { - "cell_type": "code", - "execution_count": 43, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "a => 1\n", - "b => 2\n" - ] - } - ], - "source": [ - "print_info(a=1, b=2)" - ] - }, - { - "cell_type": "code", - "execution_count": 45, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "ename": "TypeError", - "evalue": "print_info() takes 0 positional arguments but 3 were given", - "output_type": "error", - "traceback": [ - "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", - "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mprint_info\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m2\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m3\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", - "\u001b[1;31mTypeError\u001b[0m: print_info() takes 0 positional arguments but 3 were given" - ] - } - ], - "source": [ - "print_info(1, 2, 3)" - ] - }, - { - "cell_type": "code", - "execution_count": 46, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "ename": "TypeError", - "evalue": "sum() got an unexpected keyword argument 'a'", - "output_type": "error", - "traceback": [ - "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", - "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0msum\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0ma\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mb\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;36m2\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", - "\u001b[1;31mTypeError\u001b[0m: sum() got an unexpected keyword argument 'a'" - ] - } - ], - "source": [ - "sum(a=1, b=2)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "可变参数函数在定义的时候,就决定了参数是位置参数还是关键字参数" - ] - }, - { - "cell_type": "code", - "execution_count": 47, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "def print_info(*args, **kwargs):\n", - " for x in args:\n", - " print(x)\n", - " for k, v in kwargs.items():\n", - " print('{0} => {1}'.format(k, v))" - ] - }, - { - "cell_type": "code", - "execution_count": 48, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "1\n", - "2\n", - "3\n", - "a => 4\n", - "b => 5\n" - ] - } - ], - "source": [ - "print_info(1, 2, 3, a=4, b=5)" - ] - }, - { - "cell_type": "code", - "execution_count": 51, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "def print_info(x, y, *args, **kwargs):\n", - " print('x = {0}'.format(x))\n", - " print('y = {0}'.format(y))\n", - " for x in args:\n", - " print(x)\n", - " for k, v in kwargs.items():\n", - " print('{0} => {1}'.format(k, v))" - ] - }, - { - "cell_type": "code", - "execution_count": 52, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "x = 1\n", - "y = 2\n", - "4\n", - "5\n", - "6\n", - "a => 7\n", - "b => 8\n" - ] - } - ], - "source": [ - "print_info(1, 2, 4, 5, 6, a=7, b=8)" - ] - }, - { - "cell_type": "code", - "execution_count": 53, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "def print_info(*args, x, y, **kwargs):\n", - " print('x = {0}'.format(x))\n", - " print('y = {0}'.format(y))\n", - " for x in args:\n", - " print(x)\n", - " for k, v in kwargs.items():\n", - " print('{0} => {1}'.format(k, v))" - ] - }, - { - "cell_type": "code", - "execution_count": 54, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "ename": "TypeError", - "evalue": "print_info() missing 2 required keyword-only arguments: 'x' and 'y'", - "output_type": "error", - "traceback": [ - "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", - "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mprint_info\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m2\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m4\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m5\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m6\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0ma\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;36m7\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mb\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;36m8\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", - "\u001b[1;31mTypeError\u001b[0m: print_info() missing 2 required keyword-only arguments: 'x' and 'y'" - ] - } - ], - "source": [ - "print_info(1, 2, 4, 5, 6, a=7, b=8)" - ] - }, - { - "cell_type": "code", - "execution_count": 55, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "x = 5\n", - "y = 6\n", - "1\n", - "2\n", - "3\n", - "4\n", - "a => 7\n", - "b => 8\n" - ] - } - ], - "source": [ - "print_info(1,2 , 3, 4, x=5, y=6, a=7, b=8)" - ] - }, - { - "cell_type": "code", - "execution_count": 56, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "ename": "SyntaxError", - "evalue": "invalid syntax (, line 1)", - "output_type": "error", - "traceback": [ - "\u001b[1;36m File \u001b[1;32m\"\"\u001b[1;36m, line \u001b[1;32m1\u001b[0m\n\u001b[1;33m def print_info(*args, **kwargs, x, y):\u001b[0m\n\u001b[1;37m ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid syntax\n" - ] - } - ], - "source": [ - "def print_info(*args, **kwargs, x, y):\n", - " print('x = {0}'.format(x))\n", - " print('y = {0}'.format(y))\n", - " for x in args:\n", - " print(x)\n", - " for k, v in kwargs.items():\n", - " print('{0} => {1}'.format(k, v))" - ] - }, - { - "cell_type": "code", - "execution_count": 57, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "def print_info( x, y=0, *args, **kwargs):\n", - " print('x = {0}'.format(x))\n", - " print('y = {0}'.format(y))\n", - " for x in args:\n", - " print(x)\n", - " for k, v in kwargs.items():\n", - " print('{0} => {1}'.format(k, v))" - ] - }, - { - "cell_type": "code", - "execution_count": 58, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "x = 1\n", - "y = 2\n", - "4\n", - "5\n", - "6\n", - "a => 7\n", - "b => 8\n" - ] - } - ], - "source": [ - "print_info(1, 2, 4, 5, 6, a=7, b=8)" - ] - }, - { - "cell_type": "code", - "execution_count": 59, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "x = 1\n", - "y = 0\n", - "a => 7\n", - "b => 8\n" - ] - } - ], - "source": [ - "print_info(1, a=7, b=8)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "* 非默认非可变参数, 可变位置参数,可变关键字参数\n", - "* 默认参数不要和可变参数放到一起" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### 参数解包" - ] - }, - { - "cell_type": "code", - "execution_count": 60, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "def add(x, y):\n", - " print('x is {0}'.format(x))\n", - " print('y is {0}'.format(y))\n", - " return x + y" - ] - }, - { - "cell_type": "code", - "execution_count": 61, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "lst = [1, 2]" - ] - }, - { - "cell_type": "code", - "execution_count": 62, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "x is 1\n", - "y is 2\n" - ] - }, - { - "data": { - "text/plain": [ - "3" - ] - }, - "execution_count": 62, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "add(lst[0], lst[1])" - ] - }, - { - "cell_type": "code", - "execution_count": 63, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "x is 1\n", - "y is 2\n" - ] - }, - { - "data": { - "text/plain": [ - "3" - ] - }, - "execution_count": 63, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "add(*lst)" - ] - }, - { - "cell_type": "code", - "execution_count": 64, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "d = {'x': 1, 'y': 2}" - ] - }, - { - "cell_type": "code", - "execution_count": 65, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "x is 1\n", - "y is 2\n" - ] - }, - { - "data": { - "text/plain": [ - "3" - ] - }, - "execution_count": 65, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "add(**d)" - ] - }, - { - "cell_type": "code", - "execution_count": 66, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "ename": "TypeError", - "evalue": "add() takes 2 positional arguments but 4 were given", - "output_type": "error", - "traceback": [ - "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", - "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0madd\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m*\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m2\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m3\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m4\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", - "\u001b[1;31mTypeError\u001b[0m: add() takes 2 positional arguments but 4 were given" - ] - } - ], - "source": [ - "add(*[1, 2, 3, 4])" - ] - }, - { - "cell_type": "code", - "execution_count": 67, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "ename": "TypeError", - "evalue": "add() got an unexpected keyword argument 'z'", - "output_type": "error", - "traceback": [ - "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", - "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0madd\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m**\u001b[0m\u001b[1;33m{\u001b[0m\u001b[1;34m'x'\u001b[0m\u001b[1;33m:\u001b[0m \u001b[1;36m1\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m'y'\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;36m2\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m'z'\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;36m3\u001b[0m\u001b[1;33m}\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", - "\u001b[1;31mTypeError\u001b[0m: add() got an unexpected keyword argument 'z'" - ] - } - ], - "source": [ - "add(**{'x': 1, 'y':2, 'z':3})" - ] - }, - { - "cell_type": "code", - "execution_count": 68, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "ename": "TypeError", - "evalue": "add() missing 1 required positional argument: 'y'", - "output_type": "error", - "traceback": [ - "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", - "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0madd\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m**\u001b[0m\u001b[1;33m{\u001b[0m\u001b[1;34m'x'\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m}\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", - "\u001b[1;31mTypeError\u001b[0m: add() missing 1 required positional argument: 'y'" - ] - } - ], - "source": [ - "add(**{'x':1})" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### 默认参数的坑" - ] - }, - { - "cell_type": "code", - "execution_count": 69, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "def fn(lst=[]):\n", - " lst.append(1)\n", - " print(lst)" - ] - }, - { - "cell_type": "code", - "execution_count": 70, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[1]\n" - ] - } - ], - "source": [ - "fn()" - ] - }, - { - "cell_type": "code", - "execution_count": 71, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[1, 1]\n" - ] - } - ], - "source": [ - "fn()" - ] - }, - { - "cell_type": "code", - "execution_count": 72, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[1, 1, 1]\n" - ] - } - ], - "source": [ - "fn()" - ] - }, - { - "cell_type": "code", - "execution_count": 73, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[1, 1, 1, 1]\n" - ] - } - ], - "source": [ - "fn()" - ] - }, - { - "cell_type": "code", - "execution_count": 76, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "def fn(lst = None):\n", - " if lst is None:\n", - " lst = []\n", - " lst.append(1)\n", - " print(lst)" - ] - }, - { - "cell_type": "code", - "execution_count": 77, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[1]\n" - ] - } - ], - "source": [ - "fn()" - ] - }, - { - "cell_type": "code", - "execution_count": 78, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[1]\n" - ] - } - ], - "source": [ - "fn()" - ] - }, - { - "cell_type": "code", - "execution_count": 79, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[1]\n" - ] - } - ], - "source": [ - "fn()" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## 函数返回值" - ] - }, - { - "cell_type": "code", - "execution_count": 80, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "def fn():\n", - " pass" - ] - }, - { - "cell_type": "code", - "execution_count": 81, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "NoneType" - ] - }, - "execution_count": 81, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "type(fn())" - ] - }, - { - "cell_type": "code", - "execution_count": 82, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "def fn():\n", - " return;" - ] - }, - { - "cell_type": "code", - "execution_count": 83, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "NoneType" - ] - }, - "execution_count": 83, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "type(fn())" - ] - }, - { - "cell_type": "code", - "execution_count": 84, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "def fn():\n", - " return 4" - ] - }, - { - "cell_type": "code", - "execution_count": 85, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "4" - ] - }, - "execution_count": 85, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "fn()" - ] - }, - { - "cell_type": "code", - "execution_count": 86, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "def fn():\n", - " return 3, 4, 5" - ] - }, - { - "cell_type": "code", - "execution_count": 87, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "(3, 4, 5)" - ] - }, - "execution_count": 87, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "fn()" - ] - }, - { - "cell_type": "code", - "execution_count": 88, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "def fn():\n", - " for x in range(10):\n", - " print(x)\n", - " if x > 5:\n", - " return" - ] - }, - { - "cell_type": "code", - "execution_count": 89, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "0\n", - "1\n", - "2\n", - "3\n", - "4\n", - "5\n", - "6\n" - ] - } - ], - "source": [ - "fn()" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### 第归函数" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "x!" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "1 * 2 * 3 * 4 ...* x" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "x * (x-1)!" - ] - }, - { - "cell_type": "code", - "execution_count": 92, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "def fn(x):\n", - " if x == 1:\n", - " return x\n", - " return fn(x - 1) * x" - ] - }, - { - "cell_type": "code", - "execution_count": 93, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "120" - ] - }, - "execution_count": 93, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "fn(5)" - ] - }, - { - "cell_type": "code", - "execution_count": 94, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "120" - ] - }, - "execution_count": 94, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "1 * 2 * 3 * 4 * 5" - ] - }, - { - "cell_type": "code", - "execution_count": 95, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000" - ] - }, - "execution_count": 95, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "fn(100)" - ] - }, - { - "cell_type": "code", - "execution_count": 97, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "67526802209645841583879061361800814224269427869589384312198268703685091643180416969132446952698303794226010370578672908593198347699886928591906501031587651846976759681112609524787093848004428636186893395272784450630354080243217646658024696659065951793757223520229235577548653833681102170973893746054649126415909143150172860721156685810655759230011450132992176454983227538696340112610447029002337004887877266387704586077293585433151612518800147764461182680822867092786694982831838641800997499819339206579415325649748486265233918911087114592440896594062675914294925816719862178374679272092637524786939036290035924271782253738059886933923447877769583003016705363339031413069155837518524761078342052635475632113169618774549275701480106933362990003732589370593557325299434734459295866728988740794174654391479926000848846686708729736713207285203712732201272410830836913052635365082888725171636081587151603468291106754640398232146673627370895934090777828827549554232436190464827998683927179246029919443251026464452337939599198528297828591122689960620361238248313158071643395848405047261412680039877733761849874447323867911712630023171745968278465780558568067035013885275080292137360491875164947724464221693533755035300065350065137490832039523382963747026185653050331832380991844842560750923543775188582096487476950254418365198999674684417286265442786651594404781622946901879166382930714196908227460133027605817864877377712193142137625430353718448269390732615776645283198828602917680224041088993892610506802195917247838900106910698057030379190571057605849323113308634452008179881165616449767648354161225066967961297609698742737923389391615207441152319392845687673311899247085327703421862972871644495409572259985563215471482083325653231777113271326579970310755604973969708949477374254974480294652427022436705380184064008853457214518515270985563195412993145274057688634448812449445800617631162768243125606424844709372022149908463572254912654907763445758543980999149122998104378965626781898655221443263601405152073199706585080288735040205417371277253096243200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000" - ] - }, - "execution_count": 97, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "fn(900)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [] - }, - { - "cell_type": "code", - "execution_count": 91, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "ename": "RuntimeError", - "evalue": "maximum recursion depth exceeded", - "output_type": "error", - "traceback": [ - "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[1;31mRuntimeError\u001b[0m Traceback (most recent call last)", - "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mfn\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m5\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", - "\u001b[1;32m\u001b[0m in \u001b[0;36mfn\u001b[1;34m(x)\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[1;32mdef\u001b[0m \u001b[0mfn\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mx\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m \u001b[1;32mreturn\u001b[0m \u001b[0mfn\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mx\u001b[0m \u001b[1;33m-\u001b[0m \u001b[1;36m1\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;33m*\u001b[0m \u001b[0mx\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", - "... last 1 frames repeated, from the frame below ...\n", - "\u001b[1;32m\u001b[0m in \u001b[0;36mfn\u001b[1;34m(x)\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[1;32mdef\u001b[0m \u001b[0mfn\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mx\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m \u001b[1;32mreturn\u001b[0m \u001b[0mfn\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mx\u001b[0m \u001b[1;33m-\u001b[0m \u001b[1;36m1\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;33m*\u001b[0m \u001b[0mx\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", - "\u001b[1;31mRuntimeError\u001b[0m: maximum recursion depth exceeded" - ] - } - ], - "source": [ - "fn(5)" - ] - }, - { - "cell_type": "code", - "execution_count": 98, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "def fn(x):\n", - " ret = 1\n", - " for i in range(1, x +1):\n", - " ret *= i\n", - " return ret" - ] - }, - { - "cell_type": "code", - "execution_count": 99, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "120" - ] - }, - "execution_count": 99, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "fn(5)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### 作用域与全局变量" - ] - }, - { - "cell_type": "code", - "execution_count": 101, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "ename": "NameError", - "evalue": "name 'xx' is not defined", - "output_type": "error", - "traceback": [ - "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[1;31mNameError\u001b[0m Traceback (most recent call last)", - "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[0;32m 2\u001b[0m \u001b[0mxx\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;36m6\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 3\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 4\u001b[1;33m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mxx\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", - "\u001b[1;31mNameError\u001b[0m: name 'xx' is not defined" - ] - } - ], - "source": [ - "def fn():\n", - " xx = 6\n", - "\n", - "print(xx)" - ] - }, - { - "cell_type": "code", - "execution_count": 102, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "xx = 6\n", - "def fn():\n", - " print(xx)" - ] - }, - { - "cell_type": "code", - "execution_count": 103, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "6\n" - ] - } - ], - "source": [ - "fn()" - ] - }, - { - "cell_type": "code", - "execution_count": 105, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "3\n", - "6\n" - ] - } - ], - "source": [ - "def fn():\n", - " xx = 3\n", - " print(xx)\n", - "fn()\n", - "print(xx)" - ] - }, - { - "cell_type": "code", - "execution_count": 110, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "8978880\n", - "8978784\n", - "3\n", - "6\n" - ] - } - ], - "source": [ - "xx= 6\n", - "print(id(xx))\n", - "def fn():\n", - " xx = 3\n", - " print(id(xx))\n", - " print(xx)\n", - "fn()\n", - "print(xx)" - ] - }, - { - "cell_type": "code", - "execution_count": 111, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "3\n", - "3\n" - ] - } - ], - "source": [ - "xx = 6\n", - "def fn():\n", - " global xx\n", - " xx = 3\n", - " print(xx)\n", - "fn()\n", - "print(xx)" - ] - }, - { - "cell_type": "code", - "execution_count": 112, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "{'_i80': 'def fn():\\n pass', '_24': 3, '_i71': 'fn()', '_i2': 'fn()', '_i31': 'inc(5, 3)', 'inc': , '_i90': 'def fn(x):\\n return fn(x - 1) * x', '_i15': 'def append(item):\\n lst.append(item)\\n ', '__spec__': None, '_i99': 'fn(5)', '_i93': 'fn(5)', '_65': 3, '_i81': 'type(fn())', '_i33': 'def sum(lst):\\n ret = 0\\n for x in lst:\\n ret += x\\n return ret', '_i7': 'def add(x, y):\\n return x + y', '_i17': 'lst', '_i88': 'def fn():\\n for x in range(10):\\n print(x)\\n if x > 5:\\n return', '_i104': 'def fn():\\n xx += 3\\n print(xx)\\nfn()\\nprint(xx)', '_i109': 'xx= 6\\ndef fn():\\n print(id(xx))\\n xx = 3\\n print(id(xx))\\n print(xx)\\nfn()\\nprint(xx)', '_i98': 'def fn(x):\\n ret = 1\\n for i in range(1, x +1):\\n ret *= i\\n return ret', '_i12': 'x', '_i75': 'fn()', 'x': 1, '_i10': 'def swap(x, y):\\n x, y = y, x', 'fn': , '_i28': 'def inc(x, i=1):\\n return x + i', '_i82': 'def fn():\\n return;', '_i77': 'fn()', '_i112': 'xx = 6\\ndef fn():\\n x = 5\\n print(globals())\\n print(locals())\\n\\nfn()', '_i105': 'def fn():\\n xx = 3\\n print(xx)\\nfn()\\nprint(xx)', 'lst': [1, 2], '_i106': 'def fn():\\n print(id(xx))\\n xx = 3\\n print(id(xx))\\n print(xx)\\nfn()\\nprint(xx)', 'Out': {65: 3, 81: , 8: 3, 87: (3, 4, 5), 12: 1, 13: 2, 17: [1, 2, 3, 4], 19: 3, 20: 3, 21: 3, 23: 3, 24: 3, 25: 3, 26: 3, 29: 6, 94: 120, 31: 8, 97: 67526802209645841583879061361800814224269427869589384312198268703685091643180416969132446952698303794226010370578672908593198347699886928591906501031587651846976759681112609524787093848004428636186893395272784450630354080243217646658024696659065951793757223520229235577548653833681102170973893746054649126415909143150172860721156685810655759230011450132992176454983227538696340112610447029002337004887877266387704586077293585433151612518800147764461182680822867092786694982831838641800997499819339206579415325649748486265233918911087114592440896594062675914294925816719862178374679272092637524786939036290035924271782253738059886933923447877769583003016705363339031413069155837518524761078342052635475632113169618774549275701480106933362990003732589370593557325299434734459295866728988740794174654391479926000848846686708729736713207285203712732201272410830836913052635365082888725171636081587151603468291106754640398232146673627370895934090777828827549554232436190464827998683927179246029919443251026464452337939599198528297828591122689960620361238248313158071643395848405047261412680039877733761849874447323867911712630023171745968278465780558568067035013885275080292137360491875164947724464221693533755035300065350065137490832039523382963747026185653050331832380991844842560750923543775188582096487476950254418365198999674684417286265442786651594404781622946901879166382930714196908227460133027605817864877377712193142137625430353718448269390732615776645283198828602917680224041088993892610506802195917247838900106910698057030379190571057605849323113308634452008179881165616449767648354161225066967961297609698742737923389391615207441152319392845687673311899247085327703421862972871644495409572259985563215471482083325653231777113271326579970310755604973969708949477374254974480294652427022436705380184064008853457214518515270985563195412993145274057688634448812449445800617631162768243125606424844709372022149908463572254912654907763445758543980999149122998104378965626781898655221443263601405152073199706585080288735040205417371277253096243200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000, 34: 15, 99: 120, 36: 15, 38: 15, 39: 15, 93: 120, 83: , 85: 4, 95: 93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000, 62: 3, 63: 3}, '_i87': 'fn()', '_13': 2, '_i92': 'def fn(x):\\n if x == 1:\\n return x\\n return fn(x - 1) * x', '_i84': 'def fn():\\n return 4', '_i100': 'def fn():\\n x = 6\\n\\nprint(x)', 'In': ['', 'def fn():\\n pass', 'fn()', \"def fn():\\n print('execute fn')\", 'fn()', 'a = fn()', 'a', 'def add(x, y):\\n return x + y', 'add(1, 2)', 'x = 1\\ny = 2', 'def swap(x, y):\\n x, y = y, x', 'swap(x, y)', 'x', 'y', 'lst = [1, 2, 3]', 'def append(item):\\n lst.append(item)\\n ', 'append(4)', 'lst', 'def add(x, y):\\n return x + y', 'add(1, 2)', 'add(x=1, y=2)', 'add(y=2, x=1)', 'def add(x, y):\\n print(\"x = {0}\".format(x))\\n print(\"y = {0}\".format(y))\\n return x + y', 'add(1, 2)', 'add(x=1, y=2)', 'add(y=2, x=1)', 'add(1, y=2)', 'add(x=1, 2)', 'def inc(x, i=1):\\n return x + i', 'inc(5)', 'def inc(5, 3)', 'inc(5, 3)', 'def inc(i=1, x):\\n return x + 1', 'def sum(lst):\\n ret = 0\\n for x in lst:\\n ret += x\\n return ret', 'sum([1, 2, 3, 4, 5])', 'def sum(*args):\\n ret = 0\\n for x in args:\\n ret += x\\n return ret', 'sum(1, 2, 3, 4, 5)', 'def sum(*args):\\n ret = 0\\n print(args)\\n for x in args:\\n ret += x\\n return ret', 'sum(1, 2, 3, 4, 5)', 'sum(5, 4, 3, 2, 1)', \"def print_info(**kwargs):\\n for k, v in kwargs.items():\\n print('{0} => {1}'.format(k, w))\", 'print_info(a=1, b=2)', \"def print_info(**kwargs):\\n for k, v in kwargs.items():\\n print('{0} => {1}'.format(k, v))\", 'print_info(a=1, b=2)', '* 可变关键字参数', 'print_info(1, 2, 3)', 'sum(a=1, b=2)', \"def print_info(*args, **kwargs):\\n for x in args:\\n print(x)\\n for k, v in kwargs.items():\\n print('{0} => {1}'.format(k, v))\", 'print_info(1, 2, 3, a=4, b=5)', \"def print_info(x, y, *args, **kwargs):\\n print('x = {0}'.format(x))\\n print('y = {1}'.format(y))\\n for x in args:\\n print(x)\\n for k, v in kwargs.items():\\n print('{0} => {1}'.format(k, v))\", 'print_info(1, 2, 4, 5, 6, a=7, b=8)', \"def print_info(x, y, *args, **kwargs):\\n print('x = {0}'.format(x))\\n print('y = {0}'.format(y))\\n for x in args:\\n print(x)\\n for k, v in kwargs.items():\\n print('{0} => {1}'.format(k, v))\", 'print_info(1, 2, 4, 5, 6, a=7, b=8)', \"def print_info(*args, x, y, **kwargs):\\n print('x = {0}'.format(x))\\n print('y = {0}'.format(y))\\n for x in args:\\n print(x)\\n for k, v in kwargs.items():\\n print('{0} => {1}'.format(k, v))\", 'print_info(1, 2, 4, 5, 6, a=7, b=8)', 'print_info(1,2 , 3, 4, x=5, y=6, a=7, b=8)', \"def print_info(*args, **kwargs, x, y):\\n print('x = {0}'.format(x))\\n print('y = {0}'.format(y))\\n for x in args:\\n print(x)\\n for k, v in kwargs.items():\\n print('{0} => {1}'.format(k, v))\", \"def print_info( x, y=0, *args, **kwargs):\\n print('x = {0}'.format(x))\\n print('y = {0}'.format(y))\\n for x in args:\\n print(x)\\n for k, v in kwargs.items():\\n print('{0} => {1}'.format(k, v))\", 'print_info(1, 2, 4, 5, 6, a=7, b=8)', 'print_info(1, a=7, b=8)', \"def add(x, y):\\n print('x is {0}'.format(x))\\n print('y is {0}'.format(y))\\n return x + y\", 'lst = [1, 2]', 'add(lst[0], lst[1])', 'add(*lst)', \"d = {'x': 1, 'y': 2}\", 'add(**d)', 'add(*[1, 2, 3, 4])', \"add(**{'x': 1, 'y':2, 'z':3})\", \"add(**{'x':1})\", 'def fn(lst=[]):\\n lst.append(1)\\n print(lst)', 'fn()', 'fn()', 'fn()', 'fn()', 'def fn(lst = None):\\n if lst is None:\\n lst = []\\n lst.append(a)\\n print(lst)', 'fn()', 'def fn(lst = None):\\n if lst is None:\\n lst = []\\n lst.append(1)\\n print(lst)', 'fn()', 'fn()', 'fn()', 'def fn():\\n pass', 'type(fn())', 'def fn():\\n return;', 'type(fn())', 'def fn():\\n return 4', 'fn()', 'def fn():\\n return 3, 4, 5', 'fn()', 'def fn():\\n for x in range(10):\\n print(x)\\n if x > 5:\\n return', 'fn()', 'def fn(x):\\n return fn(x - 1) * x', 'fn(5)', 'def fn(x):\\n if x == 1:\\n return x\\n return fn(x - 1) * x', 'fn(5)', '1 * 2 * 3 * 4 * 5', 'fn(100)', 'fn(1000)', 'fn(900)', 'def fn(x):\\n ret = 1\\n for i in range(1, x +1):\\n ret *= i\\n return ret', 'fn(5)', 'def fn():\\n x = 6\\n\\nprint(x)', 'def fn():\\n xx = 6\\n\\nprint(xx)', 'xx = 6\\ndef fn():\\n print(xx)', 'fn()', 'def fn():\\n xx += 3\\n print(xx)\\nfn()\\nprint(xx)', 'def fn():\\n xx = 3\\n print(xx)\\nfn()\\nprint(xx)', 'def fn():\\n print(id(xx))\\n xx = 3\\n print(id(xx))\\n print(xx)\\nfn()\\nprint(xx)', 'xx= 6\\ndef fn():\\n print(id(xx))\\n xx = 3\\n print(id(xx))\\n print(xx)\\nfn()\\nprint(xx)', 'xx= 6\\ndef fn():\\n print(id(xx))\\n #xx = 3\\n print(id(xx))\\n print(xx)\\nfn()\\nprint(xx)', 'xx= 6\\ndef fn():\\n print(id(xx))\\n xx = 3\\n print(id(xx))\\n print(xx)\\nfn()\\nprint(xx)', 'xx= 6\\nprint(id(xx))\\ndef fn():\\n xx = 3\\n print(id(xx))\\n print(xx)\\nfn()\\nprint(xx)', 'xx = 6\\ndef fn():\\n global xx\\n xx = 3\\n print(xx)\\nfn()\\nprint(xx)', 'xx = 6\\ndef fn():\\n x = 5\\n print(globals())\\n print(locals())\\n\\nfn()'], '__doc__': 'Automatically created module for IPython interactive environment', '_i57': \"def print_info( x, y=0, *args, **kwargs):\\n print('x = {0}'.format(x))\\n print('y = {0}'.format(y))\\n for x in args:\\n print(x)\\n for k, v in kwargs.items():\\n print('{0} => {1}'.format(k, v))\", '_19': 3, '_i69': 'def fn(lst=[]):\\n lst.append(1)\\n print(lst)', '_i21': 'add(y=2, x=1)', 'add': , '_97': 67526802209645841583879061361800814224269427869589384312198268703685091643180416969132446952698303794226010370578672908593198347699886928591906501031587651846976759681112609524787093848004428636186893395272784450630354080243217646658024696659065951793757223520229235577548653833681102170973893746054649126415909143150172860721156685810655759230011450132992176454983227538696340112610447029002337004887877266387704586077293585433151612518800147764461182680822867092786694982831838641800997499819339206579415325649748486265233918911087114592440896594062675914294925816719862178374679272092637524786939036290035924271782253738059886933923447877769583003016705363339031413069155837518524761078342052635475632113169618774549275701480106933362990003732589370593557325299434734459295866728988740794174654391479926000848846686708729736713207285203712732201272410830836913052635365082888725171636081587151603468291106754640398232146673627370895934090777828827549554232436190464827998683927179246029919443251026464452337939599198528297828591122689960620361238248313158071643395848405047261412680039877733761849874447323867911712630023171745968278465780558568067035013885275080292137360491875164947724464221693533755035300065350065137490832039523382963747026185653050331832380991844842560750923543775188582096487476950254418365198999674684417286265442786651594404781622946901879166382930714196908227460133027605817864877377712193142137625430353718448269390732615776645283198828602917680224041088993892610506802195917247838900106910698057030379190571057605849323113308634452008179881165616449767648354161225066967961297609698742737923389391615207441152319392845687673311899247085327703421862972871644495409572259985563215471482083325653231777113271326579970310755604973969708949477374254974480294652427022436705380184064008853457214518515270985563195412993145274057688634448812449445800617631162768243125606424844709372022149908463572254912654907763445758543980999149122998104378965626781898655221443263601405152073199706585080288735040205417371277253096243200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000, '_31': 8, '_63': 3, '_i101': 'def fn():\\n xx = 6\\n\\nprint(xx)', '_i46': 'sum(a=1, b=2)', '_i37': 'def sum(*args):\\n ret = 0\\n print(args)\\n for x in args:\\n ret += x\\n return ret', 'sum': , '_oh': {65: 3, 81: , 8: 3, 87: (3, 4, 5), 12: 1, 13: 2, 17: [1, 2, 3, 4], 19: 3, 20: 3, 21: 3, 23: 3, 24: 3, 25: 3, 26: 3, 29: 6, 94: 120, 31: 8, 97: 67526802209645841583879061361800814224269427869589384312198268703685091643180416969132446952698303794226010370578672908593198347699886928591906501031587651846976759681112609524787093848004428636186893395272784450630354080243217646658024696659065951793757223520229235577548653833681102170973893746054649126415909143150172860721156685810655759230011450132992176454983227538696340112610447029002337004887877266387704586077293585433151612518800147764461182680822867092786694982831838641800997499819339206579415325649748486265233918911087114592440896594062675914294925816719862178374679272092637524786939036290035924271782253738059886933923447877769583003016705363339031413069155837518524761078342052635475632113169618774549275701480106933362990003732589370593557325299434734459295866728988740794174654391479926000848846686708729736713207285203712732201272410830836913052635365082888725171636081587151603468291106754640398232146673627370895934090777828827549554232436190464827998683927179246029919443251026464452337939599198528297828591122689960620361238248313158071643395848405047261412680039877733761849874447323867911712630023171745968278465780558568067035013885275080292137360491875164947724464221693533755035300065350065137490832039523382963747026185653050331832380991844842560750923543775188582096487476950254418365198999674684417286265442786651594404781622946901879166382930714196908227460133027605817864877377712193142137625430353718448269390732615776645283198828602917680224041088993892610506802195917247838900106910698057030379190571057605849323113308634452008179881165616449767648354161225066967961297609698742737923389391615207441152319392845687673311899247085327703421862972871644495409572259985563215471482083325653231777113271326579970310755604973969708949477374254974480294652427022436705380184064008853457214518515270985563195412993145274057688634448812449445800617631162768243125606424844709372022149908463572254912654907763445758543980999149122998104378965626781898655221443263601405152073199706585080288735040205417371277253096243200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000, 34: 15, 99: 120, 36: 15, 38: 15, 39: 15, 93: 120, 83: , 85: 4, 95: 93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000, 62: 3, 63: 3}, '_i22': 'def add(x, y):\\n print(\"x = {0}\".format(x))\\n print(\"y = {0}\".format(y))\\n return x + y', '_i35': 'def sum(*args):\\n ret = 0\\n for x in args:\\n ret += x\\n return ret', '_i56': \"def print_info(*args, **kwargs, x, y):\\n print('x = {0}'.format(x))\\n print('y = {0}'.format(y))\\n for x in args:\\n print(x)\\n for k, v in kwargs.items():\\n print('{0} => {1}'.format(k, v))\", '_i30': 'def inc(5, 3)', '__loader__': None, '_i43': 'print_info(a=1, b=2)', '_i96': 'fn(1000)', '_29': 6, '_i38': 'sum(1, 2, 3, 4, 5)', '_ii': 'xx= 6\\nprint(id(xx))\\ndef fn():\\n xx = 3\\n print(id(xx))\\n print(xx)\\nfn()\\nprint(xx)', '_i44': '* 可变关键字参数', 'y': 2, '_i86': 'def fn():\\n return 3, 4, 5', '_i34': 'sum([1, 2, 3, 4, 5])', '_12': 1, 'xx': 6, '_i25': 'add(y=2, x=1)', '_i29': 'inc(5)', 'append': , '_i26': 'add(1, y=2)', '_i16': 'append(4)', '_i62': 'add(lst[0], lst[1])', '_i95': 'fn(100)', '_i32': 'def inc(i=1, x):\\n return x + 1', 'quit': , '_i5': 'a = fn()', 'swap': , '_i74': 'def fn(lst = None):\\n if lst is None:\\n lst = []\\n lst.append(a)\\n print(lst)', '_i47': \"def print_info(*args, **kwargs):\\n for x in args:\\n print(x)\\n for k, v in kwargs.items():\\n print('{0} => {1}'.format(k, v))\", '_i108': 'xx= 6\\ndef fn():\\n print(id(xx))\\n #xx = 3\\n print(id(xx))\\n print(xx)\\nfn()\\nprint(xx)', '_i78': 'fn()', '_i68': \"add(**{'x':1})\", '_i27': 'add(x=1, 2)', '_62': 3, '_i79': 'fn()', '_i110': 'xx= 6\\nprint(id(xx))\\ndef fn():\\n xx = 3\\n print(id(xx))\\n print(xx)\\nfn()\\nprint(xx)', '_i4': 'fn()', '_95': 93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000, '_34': 15, '_i3': \"def fn():\\n print('execute fn')\", '_26': 3, '_8': 3, '_i11': 'swap(x, y)', '_i45': 'print_info(1, 2, 3)', '_i6': 'a', '_i36': 'sum(1, 2, 3, 4, 5)', '_i102': 'xx = 6\\ndef fn():\\n print(xx)', '_i70': 'fn()', '_ih': ['', 'def fn():\\n pass', 'fn()', \"def fn():\\n print('execute fn')\", 'fn()', 'a = fn()', 'a', 'def add(x, y):\\n return x + y', 'add(1, 2)', 'x = 1\\ny = 2', 'def swap(x, y):\\n x, y = y, x', 'swap(x, y)', 'x', 'y', 'lst = [1, 2, 3]', 'def append(item):\\n lst.append(item)\\n ', 'append(4)', 'lst', 'def add(x, y):\\n return x + y', 'add(1, 2)', 'add(x=1, y=2)', 'add(y=2, x=1)', 'def add(x, y):\\n print(\"x = {0}\".format(x))\\n print(\"y = {0}\".format(y))\\n return x + y', 'add(1, 2)', 'add(x=1, y=2)', 'add(y=2, x=1)', 'add(1, y=2)', 'add(x=1, 2)', 'def inc(x, i=1):\\n return x + i', 'inc(5)', 'def inc(5, 3)', 'inc(5, 3)', 'def inc(i=1, x):\\n return x + 1', 'def sum(lst):\\n ret = 0\\n for x in lst:\\n ret += x\\n return ret', 'sum([1, 2, 3, 4, 5])', 'def sum(*args):\\n ret = 0\\n for x in args:\\n ret += x\\n return ret', 'sum(1, 2, 3, 4, 5)', 'def sum(*args):\\n ret = 0\\n print(args)\\n for x in args:\\n ret += x\\n return ret', 'sum(1, 2, 3, 4, 5)', 'sum(5, 4, 3, 2, 1)', \"def print_info(**kwargs):\\n for k, v in kwargs.items():\\n print('{0} => {1}'.format(k, w))\", 'print_info(a=1, b=2)', \"def print_info(**kwargs):\\n for k, v in kwargs.items():\\n print('{0} => {1}'.format(k, v))\", 'print_info(a=1, b=2)', '* 可变关键字参数', 'print_info(1, 2, 3)', 'sum(a=1, b=2)', \"def print_info(*args, **kwargs):\\n for x in args:\\n print(x)\\n for k, v in kwargs.items():\\n print('{0} => {1}'.format(k, v))\", 'print_info(1, 2, 3, a=4, b=5)', \"def print_info(x, y, *args, **kwargs):\\n print('x = {0}'.format(x))\\n print('y = {1}'.format(y))\\n for x in args:\\n print(x)\\n for k, v in kwargs.items():\\n print('{0} => {1}'.format(k, v))\", 'print_info(1, 2, 4, 5, 6, a=7, b=8)', \"def print_info(x, y, *args, **kwargs):\\n print('x = {0}'.format(x))\\n print('y = {0}'.format(y))\\n for x in args:\\n print(x)\\n for k, v in kwargs.items():\\n print('{0} => {1}'.format(k, v))\", 'print_info(1, 2, 4, 5, 6, a=7, b=8)', \"def print_info(*args, x, y, **kwargs):\\n print('x = {0}'.format(x))\\n print('y = {0}'.format(y))\\n for x in args:\\n print(x)\\n for k, v in kwargs.items():\\n print('{0} => {1}'.format(k, v))\", 'print_info(1, 2, 4, 5, 6, a=7, b=8)', 'print_info(1,2 , 3, 4, x=5, y=6, a=7, b=8)', \"def print_info(*args, **kwargs, x, y):\\n print('x = {0}'.format(x))\\n print('y = {0}'.format(y))\\n for x in args:\\n print(x)\\n for k, v in kwargs.items():\\n print('{0} => {1}'.format(k, v))\", \"def print_info( x, y=0, *args, **kwargs):\\n print('x = {0}'.format(x))\\n print('y = {0}'.format(y))\\n for x in args:\\n print(x)\\n for k, v in kwargs.items():\\n print('{0} => {1}'.format(k, v))\", 'print_info(1, 2, 4, 5, 6, a=7, b=8)', 'print_info(1, a=7, b=8)', \"def add(x, y):\\n print('x is {0}'.format(x))\\n print('y is {0}'.format(y))\\n return x + y\", 'lst = [1, 2]', 'add(lst[0], lst[1])', 'add(*lst)', \"d = {'x': 1, 'y': 2}\", 'add(**d)', 'add(*[1, 2, 3, 4])', \"add(**{'x': 1, 'y':2, 'z':3})\", \"add(**{'x':1})\", 'def fn(lst=[]):\\n lst.append(1)\\n print(lst)', 'fn()', 'fn()', 'fn()', 'fn()', 'def fn(lst = None):\\n if lst is None:\\n lst = []\\n lst.append(a)\\n print(lst)', 'fn()', 'def fn(lst = None):\\n if lst is None:\\n lst = []\\n lst.append(1)\\n print(lst)', 'fn()', 'fn()', 'fn()', 'def fn():\\n pass', 'type(fn())', 'def fn():\\n return;', 'type(fn())', 'def fn():\\n return 4', 'fn()', 'def fn():\\n return 3, 4, 5', 'fn()', 'def fn():\\n for x in range(10):\\n print(x)\\n if x > 5:\\n return', 'fn()', 'def fn(x):\\n return fn(x - 1) * x', 'fn(5)', 'def fn(x):\\n if x == 1:\\n return x\\n return fn(x - 1) * x', 'fn(5)', '1 * 2 * 3 * 4 * 5', 'fn(100)', 'fn(1000)', 'fn(900)', 'def fn(x):\\n ret = 1\\n for i in range(1, x +1):\\n ret *= i\\n return ret', 'fn(5)', 'def fn():\\n x = 6\\n\\nprint(x)', 'def fn():\\n xx = 6\\n\\nprint(xx)', 'xx = 6\\ndef fn():\\n print(xx)', 'fn()', 'def fn():\\n xx += 3\\n print(xx)\\nfn()\\nprint(xx)', 'def fn():\\n xx = 3\\n print(xx)\\nfn()\\nprint(xx)', 'def fn():\\n print(id(xx))\\n xx = 3\\n print(id(xx))\\n print(xx)\\nfn()\\nprint(xx)', 'xx= 6\\ndef fn():\\n print(id(xx))\\n xx = 3\\n print(id(xx))\\n print(xx)\\nfn()\\nprint(xx)', 'xx= 6\\ndef fn():\\n print(id(xx))\\n #xx = 3\\n print(id(xx))\\n print(xx)\\nfn()\\nprint(xx)', 'xx= 6\\ndef fn():\\n print(id(xx))\\n xx = 3\\n print(id(xx))\\n print(xx)\\nfn()\\nprint(xx)', 'xx= 6\\nprint(id(xx))\\ndef fn():\\n xx = 3\\n print(id(xx))\\n print(xx)\\nfn()\\nprint(xx)', 'xx = 6\\ndef fn():\\n global xx\\n xx = 3\\n print(xx)\\nfn()\\nprint(xx)', 'xx = 6\\ndef fn():\\n x = 5\\n print(globals())\\n print(locals())\\n\\nfn()'], '_i8': 'add(1, 2)', '_i48': 'print_info(1, 2, 3, a=4, b=5)', '_i55': 'print_info(1,2 , 3, 4, x=5, y=6, a=7, b=8)', '_i59': 'print_info(1, a=7, b=8)', '_20': 3, '_i85': 'fn()', '_i1': 'def fn():\\n pass', '_i67': \"add(**{'x': 1, 'y':2, 'z':3})\", '___': 93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000, '_85': 4, '_25': 3, '_21': 3, '_i97': 'fn(900)', '_i64': \"d = {'x': 1, 'y': 2}\", '_i': 'xx = 6\\ndef fn():\\n global xx\\n xx = 3\\n print(xx)\\nfn()\\nprint(xx)', '_i103': 'fn()', '__builtin__': , '__builtins__': , '_39': 15, '__name__': '__main__', '_i61': 'lst = [1, 2]', '_17': [1, 2, 3, 4], '_i50': 'print_info(1, 2, 4, 5, 6, a=7, b=8)', '_93': 120, '_': 120, '_i19': 'add(1, 2)', '_i39': 'sum(5, 4, 3, 2, 1)', '_36': 15, '_i52': 'print_info(1, 2, 4, 5, 6, a=7, b=8)', '_i24': 'add(x=1, y=2)', '_iii': 'xx= 6\\ndef fn():\\n print(id(xx))\\n xx = 3\\n print(id(xx))\\n print(xx)\\nfn()\\nprint(xx)', '_i66': 'add(*[1, 2, 3, 4])', '_i89': 'fn()', '_99': 120, '_87': (3, 4, 5), '__package__': None, '_i54': 'print_info(1, 2, 4, 5, 6, a=7, b=8)', '_i42': \"def print_info(**kwargs):\\n for k, v in kwargs.items():\\n print('{0} => {1}'.format(k, v))\", '_94': 120, '_i73': 'fn()', '_i14': 'lst = [1, 2, 3]', '_i76': 'def fn(lst = None):\\n if lst is None:\\n lst = []\\n lst.append(1)\\n print(lst)', '_i107': 'xx= 6\\ndef fn():\\n print(id(xx))\\n xx = 3\\n print(id(xx))\\n print(xx)\\nfn()\\nprint(xx)', '_i91': 'fn(5)', '_i13': 'y', '_i41': 'print_info(a=1, b=2)', '_i58': 'print_info(1, 2, 4, 5, 6, a=7, b=8)', 'get_ipython': >, '_i65': 'add(**d)', '_i40': \"def print_info(**kwargs):\\n for k, v in kwargs.items():\\n print('{0} => {1}'.format(k, w))\", '_81': , '_i49': \"def print_info(x, y, *args, **kwargs):\\n print('x = {0}'.format(x))\\n print('y = {1}'.format(y))\\n for x in args:\\n print(x)\\n for k, v in kwargs.items():\\n print('{0} => {1}'.format(k, v))\", '_23': 3, 'a': None, '_i72': 'fn()', '_i20': 'add(x=1, y=2)', '_i83': 'type(fn())', '_sh': , '__': 67526802209645841583879061361800814224269427869589384312198268703685091643180416969132446952698303794226010370578672908593198347699886928591906501031587651846976759681112609524787093848004428636186893395272784450630354080243217646658024696659065951793757223520229235577548653833681102170973893746054649126415909143150172860721156685810655759230011450132992176454983227538696340112610447029002337004887877266387704586077293585433151612518800147764461182680822867092786694982831838641800997499819339206579415325649748486265233918911087114592440896594062675914294925816719862178374679272092637524786939036290035924271782253738059886933923447877769583003016705363339031413069155837518524761078342052635475632113169618774549275701480106933362990003732589370593557325299434734459295866728988740794174654391479926000848846686708729736713207285203712732201272410830836913052635365082888725171636081587151603468291106754640398232146673627370895934090777828827549554232436190464827998683927179246029919443251026464452337939599198528297828591122689960620361238248313158071643395848405047261412680039877733761849874447323867911712630023171745968278465780558568067035013885275080292137360491875164947724464221693533755035300065350065137490832039523382963747026185653050331832380991844842560750923543775188582096487476950254418365198999674684417286265442786651594404781622946901879166382930714196908227460133027605817864877377712193142137625430353718448269390732615776645283198828602917680224041088993892610506802195917247838900106910698057030379190571057605849323113308634452008179881165616449767648354161225066967961297609698742737923389391615207441152319392845687673311899247085327703421862972871644495409572259985563215471482083325653231777113271326579970310755604973969708949477374254974480294652427022436705380184064008853457214518515270985563195412993145274057688634448812449445800617631162768243125606424844709372022149908463572254912654907763445758543980999149122998104378965626781898655221443263601405152073199706585080288735040205417371277253096243200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000, '_i23': 'add(1, 2)', 'exit': , '_dh': ['/home/comyn/magedu/python/2016/004'], '_i51': \"def print_info(x, y, *args, **kwargs):\\n print('x = {0}'.format(x))\\n print('y = {0}'.format(y))\\n for x in args:\\n print(x)\\n for k, v in kwargs.items():\\n print('{0} => {1}'.format(k, v))\", '_i94': '1 * 2 * 3 * 4 * 5', '_38': 15, 'print_info': , '_i18': 'def add(x, y):\\n return x + y', '_i111': 'xx = 6\\ndef fn():\\n global xx\\n xx = 3\\n print(xx)\\nfn()\\nprint(xx)', '_i60': \"def add(x, y):\\n print('x is {0}'.format(x))\\n print('y is {0}'.format(y))\\n return x + y\", '_83': , '_i53': \"def print_info(*args, x, y, **kwargs):\\n print('x = {0}'.format(x))\\n print('y = {0}'.format(y))\\n for x in args:\\n print(x)\\n for k, v in kwargs.items():\\n print('{0} => {1}'.format(k, v))\", 'd': {'y': 2, 'x': 1}, '_i63': 'add(*lst)', '_i9': 'x = 1\\ny = 2'}\n", - "{'x': 5}\n" - ] - } - ], - "source": [ - "xx = 6\n", - "def fn():\n", - " x = 5\n", - " print(globals())\n", - " print(locals())\n", - "\n", - "fn()" - ] - }, - { - "cell_type": "code", - "execution_count": 113, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "def fn():\n", - " x = 5\n", - " print(locals())" - ] - }, - { - "cell_type": "code", - "execution_count": 114, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "{'x': 5}\n" - ] - } - ], - "source": [ - "fn()" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## 函数的调用过程" - ] - }, - { - "cell_type": "code", - "execution_count": 115, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "def add(x, y):\n", - " return x + y\n", - "\n", - "def inc(x):\n", - " return x+1\n", - "\n", - "def main():\n", - " x = 1\n", - " y = 2\n", - " ret = add(x, y)\n", - " ret = inc(ret)\n", - " print(ret)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "def add(x, y):\n", - " return x + y\n", - "\n", - "def inc(x):\n", - " return add(x, 1)\n", - "\n", - "def main():\n", - " x = 5\n", - " inc(x)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## 生成器" - ] - }, - { - "cell_type": "code", - "execution_count": 116, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "[0, 1, 2, 3, 4]" - ] - }, - "execution_count": 116, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "def iterator(x):\n", - " return list(range(x))\n", - "\n", - "iterator(5)" - ] - }, - { - "cell_type": "code", - "execution_count": 117, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "def iterator(x):\n", - " i = 0;\n", - " while i < 5:\n", - " yield i\n", - " i += 1" - ] - }, - { - "cell_type": "code", - "execution_count": 118, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "" - ] - }, - "execution_count": 118, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "iterator(5)" - ] - }, - { - "cell_type": "code", - "execution_count": 120, - "metadata": { - "collapsed": false - }, - "outputs": [], - "source": [ - "def main():\n", - " for i in iterator(5):\n", - " print(i)" - ] - }, - { - "cell_type": "code", - "execution_count": 121, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "def iterrator():\n", - " lst = [1, 2, 3, 4, 5]\n", - " yield from lst" - ] - }, - { - "cell_type": "code", - "execution_count": 122, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "1\n", - "2\n", - "3\n", - "4\n", - "5\n" - ] - } - ], - "source": [ - "for x in iterrator():\n", - " print(x)" - ] - }, - { - "cell_type": "code", - "execution_count": 125, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "def fn(x):\n", - " ret = 1\n", - " for i in range(1, x+1):\n", - " ret *= i\n", - " yield ret\n", - " " - ] - }, - { - "cell_type": "code", - "execution_count": 126, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "1\n", - "2\n", - "6\n", - "24\n", - "120\n" - ] - } - ], - "source": [ - "for x in fn(5):\n", - " print(x)" - ] - }, - { - "cell_type": "code", - "execution_count": 127, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "" - ] - }, - "execution_count": 127, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "fn(5)" - ] - }, - { - "cell_type": "code", - "execution_count": 128, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "it = fn(5)" - ] - }, - { - "cell_type": "code", - "execution_count": 129, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "1" - ] - }, - "execution_count": 129, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "next(it)" - ] - }, - { - "cell_type": "code", - "execution_count": 130, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "2" - ] - }, - "execution_count": 130, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "next(it)" - ] - }, - { - "cell_type": "code", - "execution_count": 131, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "6" - ] - }, - "execution_count": 131, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "next(it)" - ] - }, - { - "cell_type": "code", - "execution_count": 132, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "24" - ] - }, - "execution_count": 132, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "next(it)" - ] - }, - { - "cell_type": "code", - "execution_count": 133, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "120" - ] - }, - "execution_count": 133, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "next(it)" - ] - }, - { - "cell_type": "code", - "execution_count": 134, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "ename": "StopIteration", - "evalue": "", - "output_type": "error", - "traceback": [ - "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[1;31mStopIteration\u001b[0m Traceback (most recent call last)", - "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mnext\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mit\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", - "\u001b[1;31mStopIteration\u001b[0m: " - ] - } - ], - "source": [ - "next(it)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "collapsed": true - }, - "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.4.2" - } - }, - "nbformat": 4, - "nbformat_minor": 0 -} diff --git "a/004/\350\243\205\351\245\260\345\231\250.ipynb" "b/004/\350\243\205\351\245\260\345\231\250.ipynb" deleted file mode 100644 index 6b822eb..0000000 --- "a/004/\350\243\205\351\245\260\345\231\250.ipynb" +++ /dev/null @@ -1,2785 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# 函数是一定公民" - ] - }, - { - "cell_type": "code", - "execution_count": 1, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "" - ] - }, - "execution_count": 1, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "enumerate([1, 2, 3, 4])" - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "0\n", - "1\n", - "1\n", - "2\n", - "2\n", - "3\n", - "3\n", - "4\n" - ] - } - ], - "source": [ - "for idx , item in enumerate([1,2 , 3, 4]):\n", - " print(idx)\n", - " print(item)" - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Help on method_descriptor:\n", - "\n", - "insert(...)\n", - " L.insert(index, object) -- insert object before index\n", - "\n" - ] - } - ], - "source": [ - "help(list.insert)" - ] - }, - { - "cell_type": "code", - "execution_count": 18, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "def cmp(x, y):\n", - " return x >= y\n", - "\n", - "def sort(*args):\n", - " ret = []\n", - " for item in args:\n", - " for i, v in enumerate(ret):\n", - " if cmp(item, v):\n", - " ret.insert(i, item)\n", - " break\n", - " else:\n", - " ret.append(item)\n", - " return ret\n", - " " - ] - }, - { - "cell_type": "code", - "execution_count": 12, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "[5, 3, 2, 1]" - ] - }, - "execution_count": 12, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "sort(True, 3, 1, 2, 5)" - ] - }, - { - "cell_type": "code", - "execution_count": 13, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "[1, 2, 3, 5]" - ] - }, - "execution_count": 13, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "sort(False, 3, 1, 2, 5)" - ] - }, - { - "cell_type": "code", - "execution_count": 19, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "[5, 3, 2, 1]" - ] - }, - "execution_count": 19, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "sort(3, 1, 2, 5)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## 高阶函数" - ] - }, - { - "cell_type": "code", - "execution_count": 20, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "def sort(cmp, *args):\n", - " ret = []\n", - " for item in args:\n", - " for i, v in enumerate(ret):\n", - " if cmp(item, v):\n", - " ret.insert(i, item)\n", - " break\n", - " else:\n", - " ret.append(item)\n", - " return ret" - ] - }, - { - "cell_type": "code", - "execution_count": 21, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "def cmp1(x, y):\n", - " return x >= y\n", - "\n", - "def cmp2(x, y):\n", - " return x <= y" - ] - }, - { - "cell_type": "code", - "execution_count": 22, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "[5, 3, 2, 1]" - ] - }, - "execution_count": 22, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "sort(cmp1, 3, 1, 2, 5)" - ] - }, - { - "cell_type": "code", - "execution_count": 23, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "[1, 2, 3, 5]" - ] - }, - "execution_count": 23, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "sort(cmp2, 3, 1, 2, 5)" - ] - }, - { - "cell_type": "code", - "execution_count": 24, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Help on class filter in module builtins:\n", - "\n", - "class filter(object)\n", - " | filter(function or None, iterable) --> filter object\n", - " | \n", - " | Return an iterator yielding those items of iterable for which function(item)\n", - " | is true. If function is None, return the items that are true.\n", - " | \n", - " | Methods defined here:\n", - " | \n", - " | __getattribute__(self, name, /)\n", - " | Return getattr(self, name).\n", - " | \n", - " | __iter__(self, /)\n", - " | Implement iter(self).\n", - " | \n", - " | __new__(*args, **kwargs) from builtins.type\n", - " | Create and return a new object. See help(type) for accurate signature.\n", - " | \n", - " | __next__(self, /)\n", - " | Implement next(self).\n", - " | \n", - " | __reduce__(...)\n", - " | Return state information for pickling.\n", - "\n" - ] - } - ], - "source": [ - "help(filter)" - ] - }, - { - "cell_type": "code", - "execution_count": 26, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "def bigger_5(x):\n", - " if x > 5:\n", - " return True\n", - " return False" - ] - }, - { - "cell_type": "code", - "execution_count": 27, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "" - ] - }, - "execution_count": 27, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "filter(bigger_5, range(10))" - ] - }, - { - "cell_type": "code", - "execution_count": 28, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "[6, 7, 8, 9]" - ] - }, - "execution_count": 28, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "list(filter(bigger_5, range(10)))" - ] - }, - { - "cell_type": "code", - "execution_count": 29, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Help on built-in function any in module builtins:\n", - "\n", - "any(...)\n", - " any(iterable) -> bool\n", - " \n", - " Return True if bool(x) is True for any x in the iterable.\n", - " If the iterable is empty, return False.\n", - "\n" - ] - } - ], - "source": [ - "help(any)" - ] - }, - { - "cell_type": "code", - "execution_count": 30, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Help on class map in module builtins:\n", - "\n", - "class map(object)\n", - " | map(func, *iterables) --> map object\n", - " | \n", - " | Make an iterator that computes the function using arguments from\n", - " | each of the iterables. Stops when the shortest iterable is exhausted.\n", - " | \n", - " | Methods defined here:\n", - " | \n", - " | __getattribute__(self, name, /)\n", - " | Return getattr(self, name).\n", - " | \n", - " | __iter__(self, /)\n", - " | Implement iter(self).\n", - " | \n", - " | __new__(*args, **kwargs) from builtins.type\n", - " | Create and return a new object. See help(type) for accurate signature.\n", - " | \n", - " | __next__(self, /)\n", - " | Implement next(self).\n", - " | \n", - " | __reduce__(...)\n", - " | Return state information for pickling.\n", - "\n" - ] - } - ], - "source": [ - "help(map)" - ] - }, - { - "cell_type": "code", - "execution_count": 31, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "def inc(x):\n", - " return x + 1" - ] - }, - { - "cell_type": "code", - "execution_count": 32, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "ename": "TypeError", - "evalue": "'int' object is not iterable", - "output_type": "error", - "traceback": [ - "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", - "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mmap\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0minc\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m1\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m2\u001b[0m \u001b[1;33m,\u001b[0m \u001b[1;36m3\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m4\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", - "\u001b[1;31mTypeError\u001b[0m: 'int' object is not iterable" - ] - } - ], - "source": [ - "map(inc, 1,2 , 3, 4)" - ] - }, - { - "cell_type": "code", - "execution_count": 33, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "" - ] - }, - "execution_count": 33, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "map(inc, [1, 2, 3, 4])" - ] - }, - { - "cell_type": "code", - "execution_count": 34, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "[2, 3, 4, 5]" - ] - }, - "execution_count": 34, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "list(map(inc, [1, 2, 3, 4]))" - ] - }, - { - "cell_type": "code", - "execution_count": 38, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "def left_fold(fn, *args):\n", - " it = iter(args)\n", - " ret = next(it)\n", - " for x in it:\n", - " ret = fn(ret, x)\n", - " return ret\n", - " " - ] - }, - { - "cell_type": "code", - "execution_count": 36, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "def add(x, y):\n", - " return x+y" - ] - }, - { - "cell_type": "code", - "execution_count": 39, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "10" - ] - }, - "execution_count": 39, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "left_fold(add, 1, 2,3 ,4)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## 函数作为返回值" - ] - }, - { - "cell_type": "code", - "execution_count": 40, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "def make_inc(f=1):\n", - " def inc(x):\n", - " return x + f\n", - " return inc" - ] - }, - { - "cell_type": "code", - "execution_count": 41, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "inc1 = make_inc(1)" - ] - }, - { - "cell_type": "code", - "execution_count": 42, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "function" - ] - }, - "execution_count": 42, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "type(inc1)" - ] - }, - { - "cell_type": "code", - "execution_count": 43, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "6" - ] - }, - "execution_count": 43, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "inc1(5)" - ] - }, - { - "cell_type": "code", - "execution_count": 44, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "inc2 = make_inc(2)" - ] - }, - { - "cell_type": "code", - "execution_count": 45, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "7" - ] - }, - "execution_count": 45, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "inc2(5)" - ] - }, - { - "cell_type": "code", - "execution_count": 46, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "6" - ] - }, - "execution_count": 46, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "make_inc(1)(5)" - ] - }, - { - "cell_type": "code", - "execution_count": 47, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "def inc(f, x):\n", - " return x + f" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "柯里化" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "`f(x, y) -> g(x)(y)`" - ] - }, - { - "cell_type": "code", - "execution_count": 49, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "def bigger(x):\n", - " def inner_bigger(y):\n", - " return y > x\n", - " return inner_bigger" - ] - }, - { - "cell_type": "code", - "execution_count": 50, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "[6, 7, 8, 9]" - ] - }, - "execution_count": 50, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "list(filter(bigger(5), range(10)))" - ] - }, - { - "cell_type": "code", - "execution_count": 51, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "[4, 5, 6, 7, 8, 9]" - ] - }, - "execution_count": 51, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "list(filter(bigger(3), range(10)))" - ] - }, - { - "cell_type": "code", - "execution_count": 52, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "bigger_3 = bigger(3)" - ] - }, - { - "cell_type": "code", - "execution_count": 53, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "True" - ] - }, - "execution_count": 53, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "bigger_3(4)" - ] - }, - { - "cell_type": "code", - "execution_count": 54, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "False" - ] - }, - "execution_count": 54, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "bigger_3(2)" - ] - }, - { - "cell_type": "code", - "execution_count": 55, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "[4, 5, 6, 7, 8, 9]" - ] - }, - "execution_count": 55, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "list(filter(bigger_3, range(10)))" - ] - }, - { - "cell_type": "code", - "execution_count": 56, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "True" - ] - }, - "execution_count": 56, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "bigger(3)(7)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "*函数是一等对象 - 它和普通的对象(变量)是一样的*" - ] - }, - { - "cell_type": "code", - "execution_count": 57, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "def inc(f, x):\n", - " return x + f" - ] - }, - { - "cell_type": "code", - "execution_count": 58, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "def bigger(x, y):\n", - " return x > y" - ] - }, - { - "cell_type": "code", - "execution_count": 59, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "from functools import partial" - ] - }, - { - "cell_type": "code", - "execution_count": 60, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Help on class partial in module functools:\n", - "\n", - "class partial(builtins.object)\n", - " | partial(func, *args, **keywords) - new function with partial application\n", - " | of the given arguments and keywords.\n", - " | \n", - " | Methods defined here:\n", - " | \n", - " | __call__(self, /, *args, **kwargs)\n", - " | Call self as a function.\n", - " | \n", - " | __delattr__(self, name, /)\n", - " | Implement delattr(self, name).\n", - " | \n", - " | __getattribute__(self, name, /)\n", - " | Return getattr(self, name).\n", - " | \n", - " | __new__(*args, **kwargs) from builtins.type\n", - " | Create and return a new object. See help(type) for accurate signature.\n", - " | \n", - " | __reduce__(...)\n", - " | \n", - " | __repr__(self, /)\n", - " | Return repr(self).\n", - " | \n", - " | __setattr__(self, name, value, /)\n", - " | Implement setattr(self, name, value).\n", - " | \n", - " | __setstate__(...)\n", - " | \n", - " | ----------------------------------------------------------------------\n", - " | Data descriptors defined here:\n", - " | \n", - " | __dict__\n", - " | \n", - " | args\n", - " | tuple of arguments to future partial calls\n", - " | \n", - " | func\n", - " | function object to use in future partial calls\n", - " | \n", - " | keywords\n", - " | dictionary of keyword arguments to future partial calls\n", - "\n" - ] - } - ], - "source": [ - "help(partial)" - ] - }, - { - "cell_type": "code", - "execution_count": 61, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "functools.partial(, y=3)" - ] - }, - "execution_count": 61, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "partial(bigger, y=3)" - ] - }, - { - "cell_type": "code", - "execution_count": 62, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "bigger_3 = partial(bigger, y=3)" - ] - }, - { - "cell_type": "code", - "execution_count": 63, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "True" - ] - }, - "execution_count": 63, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "callable(bigger_3)" - ] - }, - { - "cell_type": "code", - "execution_count": 64, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "True" - ] - }, - "execution_count": 64, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "bigger_3(4)" - ] - }, - { - "cell_type": "code", - "execution_count": 65, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "False" - ] - }, - "execution_count": 65, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "bigger_3(2)" - ] - }, - { - "cell_type": "code", - "execution_count": 66, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "def many_args(x, y, z, a, b, c):\n", - " print('x is {}'.format(x))\n", - " print('y is {}'.format(y))\n", - " print('z is {}'.format(z))\n", - " print('a is {}'.format(a))\n", - " print('b is {}'.format(b))\n", - " print('c is {}'.format(c))" - ] - }, - { - "cell_type": "code", - "execution_count": 67, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "fn1 = partial(many_args, a=1, b=2, c=3)" - ] - }, - { - "cell_type": "code", - "execution_count": 68, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "x is 4\n", - "y is 5\n", - "z is 6\n", - "a is 1\n", - "b is 2\n", - "c is 3\n" - ] - } - ], - "source": [ - "fn1(4, 5, 6)" - ] - }, - { - "cell_type": "code", - "execution_count": 69, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "fn2 = partial(many_args, x = 1, y=2, z=3)" - ] - }, - { - "cell_type": "code", - "execution_count": 70, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "x is 4\n", - "y is 5\n", - "z is 6\n", - "a is 2\n", - "b is 2\n", - "c is 3\n" - ] - } - ], - "source": [ - "fn1(4,5, 6, a=2)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## 装饰器" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [] - }, - { - "cell_type": "code", - "execution_count": 71, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "import time" - ] - }, - { - "cell_type": "code", - "execution_count": 72, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "1456624046.828106" - ] - }, - "execution_count": 72, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "time.time()" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [] - }, - { - "cell_type": "code", - "execution_count": 73, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "def timeit(fn):\n", - " start = time.time()\n", - " fn()\n", - " print(time.time() - start)" - ] - }, - { - "cell_type": "code", - "execution_count": 74, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "def sleep():\n", - " time.sleep(3)" - ] - }, - { - "cell_type": "code", - "execution_count": 75, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "3.003817319869995\n" - ] - } - ], - "source": [ - "timeit(sleep)" - ] - }, - { - "cell_type": "code", - "execution_count": 88, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "def timeit(fn):\n", - " def wrap(*args, **kwargs):\n", - " start = time.time()\n", - " ret = fn(*args, **kwargs)\n", - " print(time.time() - start)\n", - " return ret\n", - " return wrap" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [] - }, - { - "cell_type": "code", - "execution_count": 77, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "def sleep(x):\n", - " time.sleep(x)" - ] - }, - { - "cell_type": "code", - "execution_count": 80, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "3.002962589263916\n" - ] - } - ], - "source": [ - "timeit(sleep)(3)" - ] - }, - { - "cell_type": "code", - "execution_count": 81, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "fn = timeit(sleep)" - ] - }, - { - "cell_type": "code", - "execution_count": 82, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "function" - ] - }, - "execution_count": 82, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "type(fn)" - ] - }, - { - "cell_type": "code", - "execution_count": 83, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "3.0012524127960205\n" - ] - } - ], - "source": [ - "fn(3)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "这就是装饰器" - ] - }, - { - "cell_type": "code", - "execution_count": 84, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "@timeit\n", - "def sleep(x):\n", - " time.sleep(x)" - ] - }, - { - "cell_type": "code", - "execution_count": 86, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "3.006810188293457\n" - ] - } - ], - "source": [ - "sleep(3)" - ] - }, - { - "cell_type": "code", - "execution_count": 91, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "@timeit\n", - "def aad(x, y):\n", - " return x + y" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [] - }, - { - "cell_type": "code", - "execution_count": 92, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "11" - ] - }, - "execution_count": 92, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "add(5, 6)" - ] - }, - { - "cell_type": "code", - "execution_count": 93, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "1.430511474609375e-06\n" - ] - }, - { - "data": { - "text/plain": [ - "11" - ] - }, - "execution_count": 93, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "timeit(add)(5, 6)" - ] - }, - { - "cell_type": "code", - "execution_count": 94, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "@timeit\n", - "def aad(x, y):\n", - " return x + y" - ] - }, - { - "cell_type": "code", - "execution_count": 95, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "11" - ] - }, - "execution_count": 95, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "add(5, 6)" - ] - }, - { - "cell_type": "code", - "execution_count": 96, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "2.6226043701171875e-06\n" - ] - }, - { - "data": { - "text/plain": [ - "11" - ] - }, - "execution_count": 96, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "aad(5, 6)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "装饰器的本质就是一个函数, 这个函数接收一个函数作为参数, 返回一个函数, 通常, 返回的这个函数, 是对传入的函数执行前后增加了一些语句,所以叫做装饰" - ] - }, - { - "cell_type": "code", - "execution_count": 97, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "def heidong(fn):\n", - " def wrap(*args, **kwargs):\n", - " pass\n", - " return wrap" - ] - }, - { - "cell_type": "code", - "execution_count": 98, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "@heidong\n", - "def do_something():\n", - " print('run run run')" - ] - }, - { - "cell_type": "code", - "execution_count": 99, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "do_something()" - ] - }, - { - "cell_type": "code", - "execution_count": 100, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "def do_other_things():\n", - " print('go go go')" - ] - }, - { - "cell_type": "code", - "execution_count": 101, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "go go go\n" - ] - } - ], - "source": [ - "do_other_things()" - ] - }, - { - "cell_type": "code", - "execution_count": 102, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "def timeit(fn):\n", - " def wrap(*args, **kwargs):\n", - " start = time.time()\n", - " ret = fn(*args, **kwargs)\n", - " print(time.time() - start)\n", - " return ret\n", - " return wrap" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "自然时间" - ] - }, - { - "cell_type": "code", - "execution_count": 103, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "3.0039329528808594" - ] - }, - "execution_count": 103, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "start = time.time()\n", - "time.sleep(3)\n", - "time.time() - start" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "processor time" - ] - }, - { - "cell_type": "code", - "execution_count": 104, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "0.009999999999999787" - ] - }, - "execution_count": 104, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "start = time.clock()\n", - "time.sleep(3)\n", - "time.clock" - ] - }, - { - "cell_type": "code", - "execution_count": 105, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "def timeit(process_time=False):\n", - " cacl = time.clock if process_time else time.time\n", - " def inner_timeit(fn):\n", - " def wrap(*args, **kwargs):\n", - " start = cacl()\n", - " ret = fn(*args, **kwargs)\n", - " print(cacl() - start)\n", - " return ret\n", - " return wrap\n", - " return inner_timeit\n", - " " - ] - }, - { - "cell_type": "code", - "execution_count": 107, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "def sleep(x):\n", - " time.sleep(x)" - ] - }, - { - "cell_type": "code", - "execution_count": 109, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "3.004034996032715\n" - ] - } - ], - "source": [ - "timeit(False)(sleep)(3)" - ] - }, - { - "cell_type": "code", - "execution_count": 110, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "fn1 = timeit(False)" - ] - }, - { - "cell_type": "code", - "execution_count": 111, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "fn2 = fn1(sleep)" - ] - }, - { - "cell_type": "code", - "execution_count": 112, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "3.003953695297241\n" - ] - } - ], - "source": [ - "fn2(3)" - ] - }, - { - "cell_type": "code", - "execution_count": 113, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "@timeit(False)\n", - "def sleep(x):\n", - " time.sleep(x)" - ] - }, - { - "cell_type": "code", - "execution_count": 114, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "3.0035619735717773\n" - ] - } - ], - "source": [ - "sleep(3)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "def timeit(process_time=False):\n", - " cacl = time.clock if process_time else time.time\n", - " def inner_timeit(fn):\n", - " def wrap(*args, **kwargs):\n", - " start = cacl()\n", - " ret = fn(*args, **kwargs)\n", - " print(cacl() - start)\n", - " return ret\n", - " return wrap\n", - " return inner_timeit" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "def timeit(process_time=False):\n", - " cacl = time.clock if process_time else time.time\n", - " def inner_timeit(fn):\n", - " def wrap(*args, **kwargs):\n", - " start = cacl()\n", - " ret = fn(*args, **kwargs)\n", - " print(cacl() - start)\n", - " # send to monitor system\n", - " return ret\n", - " return wrap\n", - " return inner_timeit" - ] - }, - { - "cell_type": "code", - "execution_count": 115, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "fn1 = timeit(False)" - ] - }, - { - "cell_type": "code", - "execution_count": 116, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "fn2 = fn1(sleep)" - ] - }, - { - "cell_type": "code", - "execution_count": 117, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "3.006286859512329\n", - "3.00647234916687\n" - ] - } - ], - "source": [ - "fn2(3)" - ] - }, - { - "cell_type": "code", - "execution_count": 118, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "3.0040056705474854\n", - "3.004228353500366\n" - ] - } - ], - "source": [ - "timeit(False)(sleep)(3)" - ] - }, - { - "cell_type": "code", - "execution_count": 119, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "fn = timeit(False)" - ] - }, - { - "cell_type": "code", - "execution_count": 120, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "@fn\n", - "def sleep(x):\n", - " time.sleep(x)" - ] - }, - { - "cell_type": "code", - "execution_count": 122, - "metadata": { - "collapsed": false - }, - "outputs": [], - "source": [ - "@timeit(False)\n", - "def sleep(x):\n", - " time.sleep(x)" - ] - }, - { - "cell_type": "code", - "execution_count": 123, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "3.0036978721618652\n" - ] - } - ], - "source": [ - "sleep(3)" - ] - }, - { - "cell_type": "code", - "execution_count": 124, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "def make_timeit():\n", - " def timeit():\n", - " def inner_timeit(fn):\n", - " def wrap(*args, **kwargs):\n", - " return fn(*args, **kwargs)\n", - " return wrap\n", - " return inner_timeit\n", - " return timeit" - ] - }, - { - "cell_type": "code", - "execution_count": 126, - "metadata": { - "collapsed": false - }, - "outputs": [], - "source": [ - "timeit = make_timeit()\n", - "@timeit()\n", - "def fn():\n", - " pass" - ] - }, - { - "cell_type": "code", - "execution_count": 127, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "def check(allows):\n", - " def deco(fn):\n", - " def wrap(username, *args, **kwargs):\n", - " if username in allows:\n", - " return fn(username, *args, **kwargs)\n", - " return \"not allow\"\n", - " return wrap\n", - " return deco\n", - " " - ] - }, - { - "cell_type": "code", - "execution_count": 128, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "@check(['comyn', 'mage'])\n", - "def private(username):\n", - " print('xxxxxxxx')" - ] - }, - { - "cell_type": "code", - "execution_count": 129, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "xxxxxxxx\n" - ] - } - ], - "source": [ - "private('comyn')" - ] - }, - { - "cell_type": "code", - "execution_count": 130, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "'not allow'" - ] - }, - "execution_count": 130, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "private('xueming')" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [] - }, - { - "cell_type": "code", - "execution_count": 135, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "def test():\n", - " '''this is test\n", - " @return None\n", - " '''\n", - " pass" - ] - }, - { - "cell_type": "code", - "execution_count": 136, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Help on function test in module __main__:\n", - "\n", - "test()\n", - " this is test\n", - " @return None\n", - "\n" - ] - } - ], - "source": [ - "help(test)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "docstring" - ] - }, - { - "cell_type": "code", - "execution_count": 134, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Help on function test in module __main__:\n", - "\n", - "test()\n", - " this is test\n", - "\n" - ] - } - ], - "source": [ - "help(test)" - ] - }, - { - "cell_type": "code", - "execution_count": 137, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "'this is test\\n @return None\\n '" - ] - }, - "execution_count": 137, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "test.__doc__" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [] - }, - { - "cell_type": "code", - "execution_count": 132, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "'test'" - ] - }, - "execution_count": 132, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "test.__name__" - ] - }, - { - "cell_type": "code", - "execution_count": 140, - "metadata": { - "collapsed": false - }, - "outputs": [], - "source": [ - "@check(['comyn', 'mage'])\n", - "def test(username):\n", - " ''' this is test'''\n", - " pass" - ] - }, - { - "cell_type": "code", - "execution_count": 141, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "'wrap'" - ] - }, - "execution_count": 141, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "test.__name__" - ] - }, - { - "cell_type": "code", - "execution_count": 142, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "test.__doc__" - ] - }, - { - "cell_type": "code", - "execution_count": 143, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "def check(allows):\n", - " def deco(fn):\n", - " def wrap(username, *args, **kwargs):\n", - " if username in allows:\n", - " return fn(username, *args, **kwargs)\n", - " return \"not allow\"\n", - " wrap.__name__ = fn.__name__\n", - " wrap.__doc__ = fn.__doc__\n", - " return wrap\n", - " return deco\n", - " " - ] - }, - { - "cell_type": "code", - "execution_count": 144, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "@check(['comyn', 'mage'])\n", - "def test(username):\n", - " ''' this is test'''\n", - " pass" - ] - }, - { - "cell_type": "code", - "execution_count": 145, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "'test'" - ] - }, - "execution_count": 145, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "test.__name__" - ] - }, - { - "cell_type": "code", - "execution_count": 146, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Help on function test in module __main__:\n", - "\n", - "test(username, *args, **kwargs)\n", - " this is test\n", - "\n" - ] - } - ], - "source": [ - "help(test)" - ] - }, - { - "cell_type": "code", - "execution_count": 147, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "from functools import wraps" - ] - }, - { - "cell_type": "code", - "execution_count": 148, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Help on function wraps in module functools:\n", - "\n", - "wraps(wrapped, assigned=('__module__', '__name__', '__qualname__', '__doc__', '__annotations__'), updated=('__dict__',))\n", - " Decorator factory to apply update_wrapper() to a wrapper function\n", - " \n", - " Returns a decorator that invokes update_wrapper() with the decorated\n", - " function as the wrapper argument and the arguments to wraps() as the\n", - " remaining arguments. Default arguments are as for update_wrapper().\n", - " This is a convenience function to simplify applying partial() to\n", - " update_wrapper().\n", - "\n" - ] - } - ], - "source": [ - "help(wraps)" - ] - }, - { - "cell_type": "code", - "execution_count": 157, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "def check(allows):\n", - " @wraps\n", - " def deco(fn):\n", - " @wraps\n", - " def wrap(username, *args, **kwargs):\n", - " if username in allows:\n", - " return fn(username, *args, **kwargs)\n", - " return \"not allow\"\n", - " return wrap\n", - " return deco" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [] - }, - { - "cell_type": "code", - "execution_count": 158, - "metadata": { - "collapsed": false - }, - "outputs": [], - "source": [ - "@check(['comyn', 'mage'])\n", - "def test(username, *args, **kwargs):\n", - " ''' this is test'''\n", - " pass" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [] - }, - { - "cell_type": "code", - "execution_count": 159, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Help on function deco in module __main__:\n", - "\n", - "deco(fn)\n", - "\n" - ] - } - ], - "source": [ - "help(test)" - ] - }, - { - "cell_type": "code", - "execution_count": 163, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "import functools\n", - "def decorator(fn):\n", - " @functools.wraps(fn)\n", - " def inner(*args, **kwargs):\n", - " return fn(*args, **kwargs)\n", - " return inner\n", - " \n", - " " - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [] - }, - { - "cell_type": "code", - "execution_count": 164, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "@decorator\n", - "def func():\n", - " '''this is test func'''\n", - " pass" - ] - }, - { - "cell_type": "code", - "execution_count": 165, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "'func'" - ] - }, - "execution_count": 165, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "func.__name__" - ] - }, - { - "cell_type": "code", - "execution_count": 166, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Help on function func in module __main__:\n", - "\n", - "func()\n", - " this is test func\n", - "\n" - ] - } - ], - "source": [ - "help(func)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [] - }, - { - "cell_type": "code", - "execution_count": 162, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Help on function update_wrapper in module functools:\n", - "\n", - "update_wrapper(wrapper, wrapped, assigned=('__module__', '__name__', '__qualname__', '__doc__', '__annotations__'), updated=('__dict__',))\n", - " Update a wrapper function to look like the wrapped function\n", - " \n", - " wrapper is the function to be updated\n", - " wrapped is the original function\n", - " assigned is a tuple naming the attributes assigned directly\n", - " from the wrapped function to the wrapper function (defaults to\n", - " functools.WRAPPER_ASSIGNMENTS)\n", - " updated is a tuple naming the attributes of the wrapper that\n", - " are updated with the corresponding attribute from the wrapped\n", - " function (defaults to functools.WRAPPER_UPDATES)\n", - "\n" - ] - } - ], - "source": [ - "help(functools.update_wrapper)" - ] - }, - { - "cell_type": "code", - "execution_count": 167, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "[5, 3, 2, 1]" - ] - }, - "execution_count": 167, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "sort(lambda x, y: x >= y, 3, 1, 2, 5)" - ] - }, - { - "cell_type": "code", - "execution_count": 168, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "func = lambda x, y: x >= y" - ] - }, - { - "cell_type": "code", - "execution_count": 169, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "[5, 3, 2, 1]" - ] - }, - "execution_count": 169, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "sort(func, 3, 1, 2, 5)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "partial" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "collapsed": true - }, - "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.4.2" - } - }, - "nbformat": 4, - "nbformat_minor": 0 -} diff --git a/notes/20160221.adoc b/notes/20160221.adoc index 380353e..1517b43 100644 --- a/notes/20160221.adoc +++ b/notes/20160221.adoc @@ -5,7 +5,7 @@ === 安装pyenv -* yum -y install git gcc make patch zlib-devel gdbm-devel openssl-devel sqlite-devel bzip2-devel readline-devel +* yum -y install git gcc make patch zlib-devel gdbm-devel openssl-devel sqlite-devel bizp2-devel readline-devel * curl -L https://raw.githubusercontent.com/yyuu/pyenv-installer/master/bin/pyenv-installer | bash === pyenv 使用 diff --git a/notes/20160227-1.adoc b/notes/20160227-1.adoc deleted file mode 100644 index 8bd3a61..0000000 --- a/notes/20160227-1.adoc +++ /dev/null @@ -1,25 +0,0 @@ -== dict - -* 初始化 -* 获取元素, 下标操作和get -* 修改元素 -* 删除元素 pop popitem del -* 遍历 keys values items - - -== 列表解析 - -* 基本语法 [expr for item in iterator] -* 带条件 -* 带多个条件的 相当and -* 多个迭代器 相当于嵌套的循环 -* 集合解析 -* 字典解析 - - -== 字符串 - -* 字符串是线性结构 切片 成员运算符 可以迭代 -* 字符串格式化 print style, format方法 -* 字符串的若干操作 join split strip replace startswith endswith -* str 和bytes区别和相互转化 Python2 和Python3处理字符串的一些区别 diff --git a/notes/20160227-2.adoc b/notes/20160227-2.adoc deleted file mode 100644 index ea096e0..0000000 --- a/notes/20160227-2.adoc +++ /dev/null @@ -1,43 +0,0 @@ -== 函数 - -=== 函数的基本语法 - -* 定义函数 -* 调用函数 - -=== 函数的参数 -* 基本语法 -* 位置参数和关键字参数 -* 默认参数 -* 可变参数 -* 参数的规则 -* 默认的坑 - -=== 函数的返回值 - -* 默认返回None -* 通过unpacking变相的返回多值 其实返回的是一个元组tuple -* 提前返回 - -=== 第归函数 - -* 调用自己的函数 -* 一定要有退出条件 -* 第归最大深度1000 -* 尽量少用第归 - -=== 变量作用域和全局变量 - -* 变量作用域 变量的可见范围 -* 全局变量 -* 局部作用域 赋值会生成新的局部变量, 除非global关键字 -* 尽量少用全局变量 -* globals() 和 locals() - -=== 函数的执行流程 - -=== 生成器 - -* 生成器的原理 -* 生成器的一些应用, 惰性求值, range xrange - diff --git a/notes/20160228.adoc b/notes/20160228.adoc deleted file mode 100644 index 32189ef..0000000 --- a/notes/20160228.adoc +++ /dev/null @@ -1,17 +0,0 @@ -== 函数作为一等公民 first class - -=== 高阶函数 接收函数作为参数 - -=== 函数作为返回值 - -* 柯里化以及应用 -* functools.partial及其应用 - -=== 装饰器 - -* 装饰器的原理 -* 带参数的装饰器 -* 现实世界中的装饰 -- 装饰器的作用 -* functools.wraps 的作用 - -=== 匿名函数 diff --git "a/presentations/005_\351\235\242\345\220\221\345\257\271\350\261\241.adoc" "b/presentations/005_\351\235\242\345\220\221\345\257\271\350\261\241.adoc" deleted file mode 100644 index f1358f2..0000000 --- "a/presentations/005_\351\235\242\345\220\221\345\257\271\350\261\241.adoc" +++ /dev/null @@ -1,370 +0,0 @@ -= 面向对象 -comyn -v1.0 -:source-highlighter: pygments -:revealjs_history: false -:revealjs_center: false -:revealjs_embedded: true -:revealjsdir: ../reveal.js -:imagesdir: ../assets/images/5 -:homepage: http://www.magedu.com - -== 编程范式 - -[%step] -* 编程范式指的是软件工程中的一种方法学 -* 常见的编程范式有:OOP、FP、PP、IP -* 通常每种语言都会提倡一些范式,也有的语言支持多种范式 -* Python是一种多范式语言,但是对OOP支持最好 - -== OOP的基本概念 - -=== 基本哲学 - -[%step] -* 世界是由对象组成的 -* 对象具有运动规律和内部状态 -* 对象之间的相互作用和通讯构成世界 - -=== 对象的特性 - -[%step] -* 唯一性:世界上没有两片相同的树叶 -* 分类性:分类是对现实世界的抽象 - -=== OOP的三大特征 - -[%step] -* 继承 -* 多态 -* 封装 - -=== 面向对象的本质 - -[%step] -* 面向对象是对数据和行为的封装 -* 但是有时候,数据仅仅是数据,方法仅仅是方法 - -== 组织数据 - -=== ! - -[source,python] ----- -data = (13, 63, 5, 378, 58, 40) - -def avg(data): - return sum(data) / len(data) - -print(avg(data)) ----- - - -=== 门的例子 - -[%step] -* 数据:门牌号、 打开/关闭的状态 -* 操作:打开、关闭 - -=== 过程化的组织方法 - -[source,python] ----- -door1 = [1, 'closed'] -door2 = [1, 'closed'] - -def open_door(door): - door[1] = 'opening' - return door - -def close_door(door): - door[1] = 'close' - return door ----- - -=== 面向对象的组织方法 - -[source,python] ----- -class Door(object): - def __init__(self, number, status): - self.number = number - self.status = status - - def open(self): - self.status = 'opening' - - def close(self): - self.status = 'closed' ----- - - -== 类的定义与初始化 - -=== 定义类 - -[%step] -[source,python] ----- -class ClassName: ----- - -[source,python] ----- -class ClassName(parents): ----- - -=== 定义方法 - -[source,python] ----- -class ClassName(object): - def method_name(self, args): # <1> <2> - pass ----- -<1> 第一个参数必须是 `self` 其他的和定义函数没有区别 -<2> `self` 是一个变量,指向对象本身。`self` 可以是任意合法的变量名 - -=== 构造方法 - -[source,python] ----- -class ClassName(object): - def __init__(self, arg): - pass ----- - -=== 实例化 - -[source,python] ----- -instance = ClassName(arg) # <1> ----- -<1> 对象的实例化,其实是调用了类的构造方法。但是 `self` 参数无需传入,解释器会自动传入。 - -=== 关于构造函数中的 `self` - -[%step] -* 我们说 `实例化其实是调用构造方法` -* 我们还说 `self指向对象本身` -* 那么,调用构造方法的时候 `self` 是什么鬼? - -=== 关于构造函数中的 `self` - -事实上, `\\__init__` 方法并非实例化时执行的第一个方法。当Python实例化一个对象时, 首先调用的是 `\\__new__` 方法,`\\__new__` 方法的第一个参数指向类本身。并且返回类的一个实例。 这个实例正是传递给 `\\__init__` 的 `self` 。所以严格的讲,`\\__new__` 方法才是真正创建实例的方法, 而 `\\__init__` 方法只是初始化实例的数据。 - - -== 访问控制 - -=== 实例变量和实例方法 - -* 任意实例方法内都可以定义定义实例变量, 通常在构造方法中定义 -* 定义在类中、不加任何装饰器,第一个参数是 `self` 的方法都叫做实例方法 -* 实例变量和实例方法绑定到特定的实例 - -=== 私有成员 - -* 以双下划线开始 -* 不以双下划线结束 - -=== 类变量和类方法 - -* 类变量定义在所有方法之外 -* 类方法使用 `@classmethod` 装饰器装饰 -* 类方法的第一个参数指向类本身, 通常使用 `cls` 命名 -* 相对于实例变量和实例方法, 类变量和类方法绑定到特定的类 - -=== 静态方法 - -* 使用 `@staticmethod` 装饰器装饰 -* 首参无特殊要求 -* 静态方法和普通的函数没有区别 - -=== 属性 - -* 使用 `@property`装饰器装饰的方法 -* 属性有 `getter` 、 `setter` 和 `deleter` - -== 继承 - -=== 单继承 - -[source,python] ----- -class A(object): - pass - -class B(A): - pass ----- - -=== 重写与super -* 子类中定义父类的同名方法,称之为重写 -* 子类中调用父类的方法, 使用super对象 -* super对象使用super方法生成 - -=== 多继承 - -[source,python] ----- -class A(object): - pass - -class B(object): - pass - -class C(A, B): - pass ----- - -=== 多继承与方法选取 - -[source,python] ----- -class A(object): - def method(self): - print('method of A') - -class B(object): - def method(self): - print('method of B') - -class C(A, B): - pass - -class D(B, A): - pass - -c = C() -c.method() - -d = D() -d.method() ----- - -=== 多继承与方法选取 - -[source,python] ----- -class A(object): - def method(self): - print('method of A') - -class B(A): - def method(self): - print('method of B') - -class C(A, B): - pass - -class D(B, A): - pass - -c = C() -c.method() -d = D() -d.method() ----- - -=== MRO - -* method resolution order -* 本地优先级: 根据声明顺序从左往右查找 -* 单调性:所有子类中,也应满足其查找顺序 - -=== C3算法 - -[source] ----- -class B(O) -> mro(B) = [B, O] -class B(A1, A2, ...) -> mro(B) = [B] + merge(mro(A1), mro(A2), ... , [A1, A2, ...]) ----- - -=== C3算法的merge步骤 - -* 顺序遍历列表 -* 首元素满足一下条件,否则遍历下一个序列 -** 在其他序列也是首元素 -** 在其他序列里不存在 -* 从所有序列中移除此元素,合并到MRO序列中 -* 重复执行,直到所有序列为空或无法执行下去 - -=== Mixin - -* mixin通过多重集成实现 -* mixin是组合的一种方式 -* mixin类通常需要满足: -** 不能单独生成实例 -** 不能继承非Mixin的类 - -== 专有方法/魔术方法 - -* 总是以双下划线开始 -* 总是以双下划线结束 - -=== 对象创建与销毁 - -* `\__new__(cls [, ...])` 创建对象 -* `\__init__(self [, ...])` 初始化对象 -* `\__del__(self)` 销毁对象时调用 - -=== 可视化对象 - -* `\\__str__(self)` 当调用 `str(obj)` 时调用此方法, 必须返回一个 `str` -* `\\__bytes__(self)` 当调用 `bytes(obj)` 时调用此方法, 必须返回一个 `bytes` -* `\\__repr__(self)` 当调用 `repr(obj)` 时调用此方法,直接print一个对象时,会自动调用 `repr` 方法 - -=== 比较运算符重载 - -[cols="6,2", options="header"] -|=== -|函数原型|运算符 -|obj.\\__lt__(self, other)| obj < other -|obj.\\__le__(self, other)| obj \<= other -|obj.\\__eq__(self, other)| obj == other -|obj.\\__ne__(self, other)| obj != other -|obj.\\__gt__(self, other)| obj > other -|obj.\\__ge__(self, other)| obj >= other -|=== - -=== `\\__hash__` 与可hash对象 - -* 当调用 `hash(obj)` 时, 事实上调用的是 `obj.\\__hash__()` -* 一个对象,实现了 `__hash__(self)` 方法, 称为可hash对象 - - -=== `\\__bool__` 方法与bool判断 - -* `\\__bool__` 方法总是返回 `True` 或 `False` -* 当定义了 `\\__bool__` 方法时, 调用 `bool(obj)` 实际调用的是 `\\__bool__` 方法 -* 当未定义 `\\__bool__` 方法时, 调用 `bool(obj)` 会调用 `\\__len__` 方法, 返回0时为False, 非0时为True -* 当 `\\__bool__` 和 `__len__` 都未定义时, `bool(obj)` 永远返回 `True` -* `if` 和 `while` 等语句的条件,如果不是bool类型, 会自动使用 `bool` 方法,转化为bool类型 - -=== `\\__call__` 与可调用对象 - -* 当一个对象实现了 `\\__call__` 方法, 那么它就是一个可调用对象 -* 可调用对象可以向函数一样调用 - - -=== `\\__enter__ \\__exit__` 与 `with` 语句 - -* `with` 语法与用途 -* `\\__enter__` 方法进入 -* `\\__exit__` 方法清理现场 - -=== 反射 - -* `\\__getattr__` 与 `\\__getattribute__` -* `\\__setattr__` -* `\\__delattr__` -* `\\__dir__` -* `\\__dict__` 与 `\\__slots__` - - -=== 描述器 - -* `\\__get__` -* `\\__set__` -* `\\__delete__` -* 用类实现装饰器