{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "

\"\"

\n", "

What the f*ck Python! \ud83d\udc0d

\n", "

\u4e00\u4e9b\u6709\u8da3\u4e14\u9c9c\u4e3a\u4eba\u77e5\u7684 Python \u7279\u6027.

\n", "\n", "\u7ffb\u8bd1\u7248\u672c: [English](https://github.com/satwikkansal/wtfpython) | [Vietnamese Ti\u1ebfng Vi\u1ec7t](https://github.com/vuduclyunitn/wtfptyhon-vi) | [Spanish Espa\u00f1ol](https://web.archive.org/web/20220511161045/https://github.com/JoseDeFreitas/wtfpython-es) | [Korean \ud55c\uad6d\uc5b4](https://github.com/buttercrab/wtfpython-ko) | [Russian \u0420\u0443\u0441\u0441\u043a\u0438\u0439](https://github.com/frontdevops/wtfpython) | [Add translation](https://github.com/satwikkansal/wtfpython/issues/new?title=Add%20translation%20for%20[LANGUAGE]&body=Expected%20time%20to%20finish:%20[X]%20weeks.%20I%27ll%20start%20working%20on%20it%20from%20[Y].)\n", "\n", "\n", "\u5176\u4ed6\u6a21\u5f0f: [Interactive](https://mybinder.org/v2/gh/robertparley/wtfpython-cn/master?labpath=irrelevant%2Fwtf.ipynb) \n", "\n", "\n", "[![WTFPL 2.0][license-image]][license-url] [![Commit id][commit-image]][commit-url] [![996.icu][996.icu-image]][996.icu-url]\n", "\n", "\n", "Python, \u662f\u4e00\u4e2a\u8bbe\u8ba1\u4f18\u7f8e\u7684\u89e3\u91ca\u578b\u9ad8\u7ea7\u8bed\u8a00, \u5b83\u63d0\u4f9b\u4e86\u5f88\u591a\u80fd\u8ba9\u7a0b\u5e8f\u5458\u611f\u5230\u8212\u9002\u7684\u529f\u80fd\u7279\u6027. \u4f46\u6709\u7684\u65f6\u5019, Python \u7684\u4e00\u4e9b\u8f93\u51fa\u7ed3\u679c\u5bf9\u4e8e\u521d\u5b66\u8005\u6765\u8bf4\u4f3c\u4e4e\u5e76\u4e0d\u662f\u90a3\u4e48\u4e00\u76ee\u4e86\u7136.\n", "\n", "\u8fd9\u4e2a\u6709\u8da3\u7684\u9879\u76ee\u610f\u5728\u6536\u96c6 Python \u4e2d\u90a3\u4e9b\u96be\u4ee5\u7406\u89e3\u548c\u53cd\u4eba\u7c7b\u76f4\u89c9\u7684\u4f8b\u5b50\u4ee5\u53ca\u9c9c\u4e3a\u4eba\u77e5\u7684\u529f\u80fd\u7279\u6027, \u5e76\u5c1d\u8bd5\u8ba8\u8bba\u8fd9\u4e9b\u73b0\u8c61\u80cc\u540e\u771f\u6b63\u7684\u539f\u7406!\n", "\n", "\u867d\u7136\u4e0b\u9762\u7684\u6709\u4e9b\u4f8b\u5b50\u5e76\u4e0d\u4e00\u5b9a\u4f1a\u8ba9\u4f60\u89c9\u5f97 WTFs, \u4f46\u5b83\u4eec\u4f9d\u7136\u6709\u53ef\u80fd\u4f1a\u544a\u8bc9\u4f60\u4e00\u4e9b\u4f60\u6240\u4e0d\u77e5\u9053\u7684 Python \u6709\u8da3\u7279\u6027. \u6211\u89c9\u5f97\u8fd9\u662f\u4e00\u79cd\u5b66\u4e60\u7f16\u7a0b\u8bed\u8a00\u5185\u90e8\u539f\u7406\u7684\u597d\u529e\u6cd5, \u800c\u4e14\u6211\u76f8\u4fe1\u4f60\u4e5f\u4f1a\u4ece\u4e2d\u83b7\u5f97\u4e50\u8da3!\n", "\n", "\u5982\u679c\u60a8\u662f\u4e00\u4f4d\u7ecf\u9a8c\u6bd4\u8f83\u4e30\u5bcc\u7684 Python \u7a0b\u5e8f\u5458, \u4f60\u53ef\u4ee5\u5c1d\u8bd5\u6311\u6218\u770b\u662f\u5426\u80fd\u4e00\u6b21\u5c31\u627e\u5230\u4f8b\u5b50\u7684\u6b63\u786e\u7b54\u6848. \u4f60\u53ef\u80fd\u5bf9\u5176\u4e2d\u7684\u4e00\u4e9b\u4f8b\u5b50\u5df2\u7ecf\u6bd4\u8f83\u719f\u6089\u4e86, \u90a3\u8fd9\u4e5f\u8bb8\u80fd\u5524\u8d77\u4f60\u5f53\u5e74\u8e29\u8fd9\u4e9b\u5751\u65f6\u7684\u751c\u871c\u56de\u5fc6 :sweat_smile:\n", "\n", "PS: \u5982\u679c\u4f60\u4e0d\u662f\u7b2c\u4e00\u6b21\u8bfb\u4e86, \u4f60\u53ef\u4ee5\u5728[\u8fd9\u91cc](https://github.com/satwikkansal/wtfpython/releases/)\u83b7\u53d6\u53d8\u52a8\u5185\u5bb9.\n", "\n", "\u90a3\u4e48, \u8ba9\u6211\u4eec\u5f00\u59cb\u5427...\n", "\n", "# Table of Contents/\u76ee\u5f55\n", "\n", "\n", "- [Table of Contents/\u76ee\u5f55](#table-of-contents\u76ee\u5f55)\n", "- [Structure of the Examples/\u793a\u4f8b\u7ed3\u6784](#structure-of-the-examples\u793a\u4f8b\u7ed3\u6784)\n", "- [Usage/\u7528\u6cd5](#usage\u7528\u6cd5)\n", "- [\ud83d\udc40 Examples/\u793a\u4f8b](#-examples\u793a\u4f8b)\n", " - [Section: Strain your brain!/\u5927\u8111\u8fd0\u52a8!](#section-strain-your-brain\u5927\u8111\u8fd0\u52a8)\n", " - [> First things first!/\u8981\u4e8b\u4f18\u5148 *](#-First-things-first!/\u8981\u4e8b\u4f18\u5148-*)\n", " - [> Strings can be tricky sometimes/\u5fae\u5999\u7684\u5b57\u7b26\u4e32 *](#-strings-can-be-tricky-sometimes\u5fae\u5999\u7684\u5b57\u7b26\u4e32-)\n", " - [> Be careful with chained operations/\u5c0f\u5fc3\u94fe\u5f0f\u64cd\u4f5c](#-be-careful-with-chained-operations\u5c0f\u5fc3\u94fe\u5f0f\u64cd\u4f5c)\n", " - [> How not to use `is` operator/\u4e3a\u4ec0\u4e48\u4e0d\u4f7f\u7528 `is` \u64cd\u4f5c\u7b26](#-How-not-to-use-is-operator/\u4e3a\u4ec0\u4e48\u4e0d\u4f7f\u7528-is-\u64cd\u4f5c\u7b26-)\n", " - [> Hash brownies/\u662f\u65f6\u5019\u6765\u70b9\u86cb\u7cd5\u4e86!](#-Hash-brownies\u662f\u65f6\u5019\u6765\u70b9\u86cb\u7cd5\u4e86)\n", " - [> Deep down, we're all the same./\u672c\u8d28\u4e0a,\u6211\u4eec\u90fd\u4e00\u6837. *](#-deep-down-were-all-the-same\u672c\u8d28\u4e0a\u6211\u4eec\u90fd\u4e00\u6837-)\n", " - [> Disorder within order/\u6709\u5e8f\u4e2d\u6f5c\u85cf\u7740\u65e0\u5e8f *](#-disorder-within-order/\u6709\u5e8f\u4e2d\u6f5c\u85cf\u7740\u65e0\u5e8f-*)\n", " - [> Keep trying.../\u4e0d\u505c\u7684try *](#-Keep-trying\u4e0d\u505c\u7684try-)\n", " - [> For what?/\u4e3a\u4ec0\u4e48?](#-for-what\u4e3a\u4ec0\u4e48)\n", " - [> Evaluation time discrepancy/\u6267\u884c\u65f6\u673a\u5dee\u5f02](#-evaluation-time-discrepancy\u6267\u884c\u65f6\u673a\u5dee\u5f02)\n", " - [> `is not ...` is not `is (not ...)` / `is not ...` \u4e0d\u662f `is (not ...)`](#-is-not--is-not-is-not-is-not--\u4e0d\u662f-is-not-)\n", " - [> A tic-tac-toe where X wins in the first attempt!/\u4e00\u8e74\u5373\u81f3!](#-a-tic-tac-toe-where-x-wins-in-the-first-attempt\u4e00\u8e74\u5373\u81f3)\n", " - [> Schr\u00f6dinger's variable/\u859b\u5b9a\u8c14\u7684\u53d8\u91cf *](#-Schr\u00f6dingers-variable\u859b\u5b9a\u8c14\u7684\u53d8\u91cf-)\n", " - [> The chicken-egg problem/\u5148\u6709\u9e21\u8fd8\u662f\u5148\u6709\u86cb *](#-the-chicken-egg-problem/\u5148\u6709\u9e21\u8fd8\u662f\u5148\u6709\u86cb-*)\n", " - [> Subclass relationships/\u5b50\u7c7b\u5173\u7cfb *](#-subclass-relationships\u5b50\u7c7b\u5173\u7cfb-)\n", " - [> Methods equality and identity/\u65b9\u6cd5\u7684\u76f8\u7b49\u6027\u548c\u552f\u4e00\u6027 *](#-Methods-equality-and-identity/\u65b9\u6cd5\u7684\u76f8\u7b49\u6027\u548c\u552f\u4e00\u6027-)\n", " - [> All-true-ation/\u8fd4\u56deTrue\u7684all\u51fd\u6570 *](#-All-true-ation/\u8fd4\u56deTrue\u7684all\u51fd\u6570-)\n", " - [> The surprising comma/\u610f\u5916\u7684\u9017\u53f7](#-the-surprising-comma\u610f\u5916\u7684\u9017\u53f7)\n", " - [> Strings and the backslashes/\u5b57\u7b26\u4e32\u4e0e\u53cd\u659c\u6760](#-strings-and-the-backslashes\u5b57\u7b26\u4e32\u4e0e\u53cd\u659c\u6760)\n", " - [> not knot!/\u522b\u7ea0\u7ed3!](#-not-knot\u522b\u7ea0\u7ed3)\n", " - [> Half triple-quoted strings/\u4e09\u4e2a\u5f15\u53f7](#-half-triple-quoted-strings\u4e09\u4e2a\u5f15\u53f7)\n", " - [> What's wrong with booleans?/\u5e03\u5c14\u4f60\u548b\u4e86?](#-whats-wrong-with-booleans\u5e03\u5c14\u4f60\u548b\u4e86)\n", " - [> Class attributes and instance attributes/\u7c7b\u5c5e\u6027\u548c\u5b9e\u4f8b\u5c5e\u6027](#-class-attributes-and-instance-attributes\u7c7b\u5c5e\u6027\u548c\u5b9e\u4f8b\u5c5e\u6027)\n", " - [> yielding None/\u751f\u6210 None](#-yielding-none\u751f\u6210-none)\n", " - [> Yielding from... return!/\u751f\u6210\u5668\u91cc\u7684return *](#-Yielding-from-return/\u751f\u6210\u5668\u91cc\u7684return-)\n", " - [> Nan-reflexivity/Nan\u7684\u81ea\u53cd\u6027](#-Nan-reflexivityNan\u7684\u81ea\u53cd\u6027)\n", " - [> Mutating the immutable!/\u5f3a\u4eba\u6240\u96be](#-mutating-the-immutable\u5f3a\u4eba\u6240\u96be)\n", " - [> The disappearing variable from outer scope/\u6d88\u5931\u7684\u5916\u90e8\u53d8\u91cf](#-the-disappearing-variable-from-outer-scope\u6d88\u5931\u7684\u5916\u90e8\u53d8\u91cf)\n", " - [> The mysterious key type conversion/\u795e\u79d8\u7684\u952e\u578b\u8f6c\u6362 *](#-the-mysterious-key-type-conversion\u795e\u79d8\u7684\u952e\u578b\u8f6c\u6362-)\n", " - [> Let's see if you can guess this?/\u770b\u770b\u4f60\u80fd\u5426\u731c\u5230\u8fd9\u4e00\u70b9?](#-lets-see-if-you-can-guess-this\u770b\u770b\u4f60\u80fd\u5426\u731c\u5230\u8fd9\u4e00\u70b9)\n", " - [> Exceeds the limit for integer string conversion/\u6574\u578b\u8f6c\u5b57\u7b26\u4e32\u8d8a\u754c](#-exceeds-the-limit-for-integer-string-conversion\u6574\u578b\u8f6c\u5b57\u7b26\u4e32\u8d8a\u754c)\n", " - [Section: Slippery Slopes/\u6ed1\u5761\u8c2c\u8bef](#section-slippery-slopes\u6ed1\u5761\u8c2c\u8bef)\n", " - [> Modifying a dictionary while iterating over it/\u8fed\u4ee3\u5b57\u5178\u65f6\u7684\u4fee\u6539](#-modifying-a-dictionary-while-iterating-over-it\u8fed\u4ee3\u5b57\u5178\u65f6\u7684\u4fee\u6539)\n", " - [> Stubborn `del` operator/\u575a\u5f3a\u7684 `del` *](#-stubborn-del-operator\u575a\u5f3a\u7684-del-)\n", " - [> The out of scope variable/\u5916\u90e8\u4f5c\u7528\u57df\u53d8\u91cf](#-the-out-of-scope-variable\u5916\u90e8\u4f5c\u7528\u57df\u53d8\u91cf)\n", " - [> Deleting a list item while iterating/\u8fed\u4ee3\u5217\u8868\u65f6\u5220\u9664\u5143\u7d20](#-deleting-a-list-item-while-iterating\u8fed\u4ee3\u5217\u8868\u65f6\u5220\u9664\u5143\u7d20)\n", " - [> Lossy zip of iterators/\u4e22\u4e09\u843d\u56db\u7684zip *](#->-Lossy-zip-of-iterators/\u4e22\u4e09\u843d\u56db\u7684zip-)\n", " - [> Loop variables leaking out!/\u5faa\u73af\u53d8\u91cf\u6cc4\u6f0f!](#-loop-variables-leaking-out\u5faa\u73af\u53d8\u91cf\u6cc4\u6f0f)\n", " - [> Beware of default mutable arguments!/\u5f53\u5fc3\u9ed8\u8ba4\u7684\u53ef\u53d8\u53c2\u6570!](#-beware-of-default-mutable-arguments\u5f53\u5fc3\u9ed8\u8ba4\u7684\u53ef\u53d8\u53c2\u6570)\n", " - [> Catching the Exceptions/\u6355\u83b7\u5f02\u5e38](#-catching-the-exceptions\u6355\u83b7\u5f02\u5e38)\n", " - [> Same operands, different story!/\u540c\u4eba\u4e0d\u540c\u547d!](#-same-operands-different-story\u540c\u4eba\u4e0d\u540c\u547d)\n", " - [> Name resolution ignoring class scope/\u5ffd\u7565\u7c7b\u4f5c\u7528\u57df\u7684\u540d\u79f0\u89e3\u6790](#-name-resolution-ignoring-class-scope\u5ffd\u7565\u7c7b\u4f5c\u7528\u57df\u7684\u540d\u79f0\u89e3\u6790)\n", " - [> Rounding like a banker/\u50cf\u94f6\u884c\u5bb6\u4e00\u6837\u820d\u5165 *](#-rounding-like-a-banker/\u50cf\u94f6\u884c\u5bb6\u4e00\u6837\u820d\u5165-)\n", " - [> Needles in a Haystack/\u5927\u6d77\u635e\u9488](#-needles-in-a-haystack\u5927\u6d77\u635e\u9488)\n", " - [> Splitsies/\u5206\u5272\u51fd\u6570](#-Splitsies\u5206\u5272\u51fd\u6570-)\n", " - [> Wild imports/\u901a\u914d\u7b26\u5bfc\u5165\u65b9\u5f0f *](#-Wild-imports\u901a\u914d\u7b26\u5bfc\u5165\u65b9\u5f0f-)\n", " - [> All sorted?/\u90fd\u6392\u5e8f\u4e86\u5417\uff1f *](#-All-sorted\u90fd\u6392\u5e8f\u4e86\u5417-)\n", " - [> Midnight time doesn't exist?/\u4e0d\u5b58\u5728\u7684\u5348\u591c?](#-midnight-time-doesnt-exist\u4e0d\u5b58\u5728\u7684\u5348\u591c)\n", " - [Section: The Hidden treasures!/\u9690\u85cf\u7684\u5b9d\u85cf!](#section-the-hidden-treasures\u9690\u85cf\u7684\u5b9d\u85cf)\n", " - [> Okay Python, Can you make me fly?/Python, \u53ef\u5426\u5e26\u6211\u98de? *](#-okay-python-can-you-make-me-flypython-\u53ef\u5426\u5e26\u6211\u98de-)\n", " - [> `goto`, but why?/`goto`, \u4f46\u4e3a\u4ec0\u4e48? *](#-goto-but-whygoto-\u4f46\u4e3a\u4ec0\u4e48-)\n", " - [> Brace yourself!/\u505a\u597d\u601d\u60f3\u51c6\u5907 *](#-brace-yourself\u505a\u597d\u601d\u60f3\u51c6\u5907-)\n", " - [> Let's meet Friendly Language Uncle For Life/\u8ba9\u751f\u6d3b\u66f4\u53cb\u597d *](#-lets-meet-friendly-language-uncle-for-life\u8ba9\u751f\u6d3b\u66f4\u53cb\u597d-)\n", " - [> Even Python understands that love is complicated/\u8fdePython\u4e5f\u77e5\u9053\u7231\u662f\u96be\u8a00\u7684 *](#-even-python-understands-that-love-is-complicated\u8fdePython\u4e5f\u77e5\u9053\u7231\u662f\u96be\u8a00\u7684-)\n", " - [> Yes, it exists!/\u662f\u7684, \u5b83\u5b58\u5728!](#-yes-it-exists\u662f\u7684-\u5b83\u5b58\u5728)\n", " - [> Ellipsis/\u7701\u7565 *](#-Ellipsis\u7701\u7565-)\n", " - [> Inpinity/\u65e0\u9650 *](#-inpinity\u65e0\u9650-)\n", " - [> Let's mangle/\u4fee\u9970\u65f6\u95f4! *](#-Lets-mangle\u4fee\u9970\u65f6\u95f4-)\n", " - [Section: Appearances are deceptive!/\u5916\u8868\u662f\u9760\u4e0d\u4f4f\u7684!](#section-appearances-are-deceptive\u5916\u8868\u662f\u9760\u4e0d\u4f4f\u7684)\n", " - [> Skipping lines?/\u8df3\u8fc7\u4e00\u884c?](#-skipping-lines\u8df3\u8fc7\u4e00\u884c)\n", " - [> Teleportation/\u7a7a\u95f4\u79fb\u52a8 *](#-teleportation\u7a7a\u95f4\u79fb\u52a8-)\n", " - [> Well, something is fishy.../\u55ef, \u6709\u4e9b\u53ef\u7591...](#-well-something-is-fishy\u55ef\u6709\u4e9b\u53ef\u7591)\n", " - [Section: Miscellaneous/\u6742\u9879](#section-miscellaneous\u6742\u9879)\n", " - [> `+=` is faster/\u66f4\u5feb\u7684 `+=` ](#--is-faster\u66f4\u5feb\u7684-)\n", " - [> Let's make a giant string!/\u6765\u505a\u4e2a\u5de8\u5927\u7684\u5b57\u7b26\u4e32\u5427!](#-lets-make-a-giant-string\u6765\u505a\u4e2a\u5de8\u5927\u7684\u5b57\u7b26\u4e32\u5427)\n", " - [> Slowing down `dict` lookups/\u8ba9\u5b57\u5178\u7684\u67e5\u627e\u6162\u4e0b\u6765 *](#-Slowing-down-dict-lookups\u8ba9\u5b57\u5178\u7684\u67e5\u627e\u6162\u4e0b\u6765-)\n", " - [> Bloating instance `dict`s/\u53d8\u81c3\u80bf\u7684`dict`\u5b9e\u4f8b\u4eec *](#-Bloating-instance-dicts/\u53d8\u81c3\u80bf\u7684dict\u5b9e\u4f8b\u4eec-)\n", " - [> Minor Ones/\u5c0f\u77e5\u8bc6\u70b9](#-minor-ones\u5c0f\u77e5\u8bc6\u70b9)\n", "- [Contributing/\u8d21\u732e](#contributing\u8d21\u732e)\n", "- [Acknowledgements/\u81f4\u8c22](#acknowledgements\u81f4\u8c22)\n", "- [\ud83c\udf93 License/\u8bb8\u53ef](#-license\u8bb8\u53ef)\n", " - [Help/\u5e2e\u52a9](#help\u5e2e\u52a9)\n", " - [Surprise your geeky pythonist friends?/\u60f3\u7ed9\u4f60\u7684\u6781\u5ba2\u670b\u53cb\u4e00\u4e2a\u60ca\u559c?](#surprise-your-geeky-pythonist-friends\u60f3\u7ed9\u4f60\u7684\u6781\u5ba2\u670b\u53cb\u4e00\u4e2a\u60ca\u559c)\n", " - [Need a pdf version?/\u9700\u8981\u6765\u4e00\u4efdpdf\u7248\u7684?](#need-a-pdf-version\u9700\u8981\u6765\u4e00\u4efdpdf\u7248\u7684)\n", " - [Follow Commit/\u8ffd\u8e2aCommit](#follow-commit\u8ffd\u8e2aCommit)\n", " - [996.icu](#996icu)\n", "\n", "\n", "\n", "# Structure of the Examples/\u793a\u4f8b\u7ed3\u6784\n", "\n", "\u6240\u6709\u793a\u4f8b\u7684\u7ed3\u6784\u90fd\u5982\u4e0b\u6240\u793a:\n", "\n", "> ### > \u4e00\u4e2a\u7cbe\u9009\u7684\u6807\u9898 *\n", "> \u6807\u9898\u672b\u5c3e\u7684\u661f\u53f7\u8868\u793a\u8be5\u793a\u4f8b\u5728\u7b2c\u4e00\u7248\u4e2d\u4e0d\u5b58\u5728\uff0c\u662f\u6700\u8fd1\u6dfb\u52a0\u7684.\n", ">\n", "> ```py\n", "> # \u51c6\u5907\u4ee3\u7801.\n", "> # \u91ca\u653e\u9b54\u6cd5...\n", "> ```\n", ">\n", "> **Output (Python version):**\n", "> ```py\n", "> >>> \u89e6\u53d1\u8bed\u53e5\n", "> \u51fa\u4e4e\u610f\u6599\u7684\u8f93\u51fa\u7ed3\u679c\n", "> ```\n", "> (\u53ef\u9009): \u5bf9\u610f\u5916\u8f93\u51fa\u7ed3\u679c\u7684\u7b80\u77ed\u63cf\u8ff0.\n", ">\n", ">\n", "> #### \ud83d\udca1 \u8bf4\u660e:\n", ">\n", "> * \u7b80\u8981\u8bf4\u660e\u53d1\u751f\u4e86\u4ec0\u4e48\u4ee5\u53ca\u4e3a\u4ec0\u4e48\u4f1a\u53d1\u751f.\n", "> ```py\n", "> \u5982\u6709\u5fc5\u8981, \u4e3e\u4f8b\u8bf4\u660e\n", "> ```\n", "> **Output:**\n", "> ```py\n", "> >>> \u89e6\u53d1\u8bed\u53e5 # \u4e00\u4e9b\u8ba9\u9b54\u6cd5\u53d8\u5f97\u5bb9\u6613\u7406\u89e3\u7684\u4f8b\u5b50\n", "> # \u4e00\u4e9b\u6b63\u5e38\u7684\u8f93\u5165\n", "> ```\n", "\n", "**\u6ce8\u610f:** \u6240\u6709\u7684\u793a\u4f8b\u90fd\u5728 Python 3.5.2 \u7248\u672c\u7684\u4ea4\u4e92\u89e3\u91ca\u5668\u4e0a\u6d4b\u8bd5\u8fc7, \u5982\u679c\u4e0d\u7279\u522b\u8bf4\u660e\u5e94\u8be5\u9002\u7528\u4e8e\u6240\u6709 Python \u7248\u672c.\n", "\n", "# Usage/\u7528\u6cd5\n", "\n", "\u6211\u4e2a\u4eba\u5efa\u8bae, \u6700\u597d\u4f9d\u6b21\u9605\u8bfb\u4e0b\u9762\u7684\u793a\u4f8b, \u5e76\u5bf9\u6bcf\u4e2a\u793a\u4f8b:\n", "- \u4ed4\u7ec6\u9605\u8bfb\u8bbe\u7f6e\u4f8b\u5b50\u6700\u5f00\u59cb\u7684\u4ee3\u7801. \u5982\u679c\u60a8\u662f\u4e00\u4f4d\u7ecf\u9a8c\u4e30\u5bcc\u7684 Python \u7a0b\u5e8f\u5458, \u90a3\u4e48\u5927\u591a\u6570\u65f6\u5019\u60a8\u90fd\u80fd\u6210\u529f\u9884\u671f\u5230\u540e\u9762\u7684\u7ed3\u679c.\n", "- \u9605\u8bfb\u8f93\u51fa\u7ed3\u679c,\n", " + \u786e\u8ba4\u7ed3\u679c\u662f\u5426\u5982\u4f60\u6240\u6599.\n", " + \u786e\u8ba4\u4f60\u662f\u5426\u77e5\u9053\u8fd9\u80cc\u540e\u7684\u539f\u7406.\n", " - \u5982\u679c\u4e0d\u77e5\u9053, \u6df1\u547c\u5438\u7136\u540e\u9605\u8bfb\u8bf4\u660e (\u5982\u679c\u4f60\u8fd8\u662f\u770b\u4e0d\u660e\u767d, \u522b\u6c89\u9ed8! \u53ef\u4ee5\u5728[\u8fd9](https://github.com/satwikkansal/wtfPython)\u63d0\u4e2a issue).\n", " - \u5982\u679c\u77e5\u9053, \u7ed9\u81ea\u5df1\u70b9\u5956\u52b1, \u7136\u540e\u53bb\u770b\u4e0b\u4e00\u4e2a\u4f8b\u5b50.\n", "\n", "PS: \u4f60\u4e5f\u53ef\u4ee5\u5728\u547d\u4ee4\u884c\u9605\u8bfb WTFpython. \u6211\u4eec\u6709 pypi \u5305 \u548c npm \u5305(\u652f\u6301\u4ee3\u7801\u9ad8\u4eae).(\u8bd1: \u8fd9\u4e24\u4e2a\u90fd\u662f\u82f1\u6587\u7248\u7684)\n", "\n", "\u5b89\u88c5 npm \u5305 [`wtfpython`](https://www.npmjs.com/package/wtfpython)\n", "```sh\n", "$ npm install -g wtfpython\n", "```\n", "\n", "\u6216\u8005, \u5b89\u88c5 pypi \u5305 [`wtfpython`](https://pypi.python.org/pypi/wtfpython)\n", "```sh\n", "$ pip install wtfpython -U\n", "```\n", "\n", "\u73b0\u5728, \u5728\u547d\u4ee4\u884c\u4e2d\u8fd0\u884c `wtfpython`, \u4f60\u5c31\u53ef\u4ee5\u5f00\u59cb\u6d4f\u89c8\u4e86.\n", "\n", "---\n", "\n", "# \ud83d\udc40 Examples/\u793a\u4f8b\n", "\n", "\n", "\n\n## \u6258\u7ba1\u7b14\u8bb0\u672c\u6307\u5357\n\n\u8fd9\u53ea\u662f\u901a\u8fc7 jupyter notebook \u6d4f\u89c8 wtfpython \u7684\u5b9e\u9a8c\u6027\u5c1d\u8bd5\u3002\u56e0\u4e3a\u4e00\u4e9b\u793a\u4f8b\u662f\u53ea\u8bfb\u7684\uff1a\n- \u5b83\u4eec\u8981\u4e48\u9700\u8981\u6258\u7ba1\u8fd0\u884c\u65f6\u4e0d\u652f\u6301\u7684 Python \u7248\u672c\u3002\n- \u8981\u4e48\u5b83\u4eec\u65e0\u6cd5\u5728\u6b64\u73af\u5883\u4e2d\u590d\u73b0\u3002\n\n\u9884\u671f\u7684\u8f93\u51fa\u5df2\u7ecf\u5b58\u5728\u4e8e\u4ee3\u7801\u5355\u5143\u4e4b\u540e\u7684\u6298\u53e0\u5355\u5143\u4e2d\u3002 Google colab \u63d0\u4f9b Python2\uff082.7\uff09\u548c Python3\uff083.6\uff0c\u9ed8\u8ba4\uff09\u8fd0\u884c\u73af\u5883\u3002 \u60a8\u53ef\u4ee5\u5728\u8fd9\u4e9b Python2 \u7279\u5b9a\u793a\u4f8b\u4e4b\u95f4\u5207\u6362\u3002 \u5bf9\u4e8e\u7279\u5b9a\u4e8e\u5176\u4ed6\u6b21\u8981\u7248\u672c\u7684\u793a\u4f8b\uff0c\u60a8\u53ef\u4ee5\u7b80\u5355\u5730\u53c2\u8003\u6298\u53e0\u7684\u8f93\u51fa\uff08\u76ee\u524d\u65e0\u6cd5\u5728\u6258\u7ba1\u7b14\u8bb0\u672c\u4e2d\u63a7\u5236\u6b21\u8981\u7248\u672c\uff09\u3002 \u60a8\u53ef\u4ee5\u68c0\u67e5\u5f53\u524d\u7684\u6d3b\u52a8\u7248\u672c\n```py\n>>> import sys\n>>> sys.version\n# Prints out Python version here.\n```\n\n\u8bdd\u867d\u5982\u6b64\uff0c\u5927\u591a\u6570\u793a\u4f8b\u90fd\u6309\u9884\u671f\u5de5\u4f5c\u3002 \u5982\u679c\u60a8\u9047\u5230\u4efb\u4f55\u95ee\u9898\uff0c\u8bf7\u968f\u65f6\u67e5\u9605 wtfpython \u4e0a\u7684\u539f\u59cb\u5185\u5bb9\u5e76\u5728 repo \u4e2d\u521b\u5efa\u95ee\u9898\u3002 \u795d\u541b\u987a\u5229\uff01\n---\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### > Strings can be tricky sometimes/\u5fae\u5999\u7684\u5b57\u7b26\u4e32 *\n", "1\\.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "140420665652016\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "a = \"some_string\"\n", "id(a)\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "140420665652016\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "id(\"some\" + \"_\" + \"string\") # \u6ce8\u610f\u4e24\u4e2a\u7684id\u503c\u662f\u76f8\u540c\u7684.\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "2\\.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "True\n", "\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "a = \"wtf\"\n", "b = \"wtf\"\n", "a is b\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "False\n", "\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "a = \"wtf!\"\n", "b = \"wtf!\"\n", "a is b\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "True # 3.7 \u7248\u672c\u8fd4\u56de\u7ed3\u679c\u4e3a False.\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "a, b = \"wtf!\", \"wtf!\"\n", "a is b \n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "3\\.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "True\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "'a' * 20 is 'aaaaaaaaaaaaaaaaaaaa'\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "False # 3.7 \u7248\u672c\u8fd4\u56de\u7ed3\u679c\u4e3a True\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "'a' * 21 is 'aaaaaaaaaaaaaaaaaaaaa'\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "\u5f88\u597d\u7406\u89e3, \u5bf9\u5427?\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### \ud83d\udca1 \u8bf4\u660e:\n", "- \u8fd9\u4e9b\u884c\u4e3a\u662f\u7531\u4e8e Cpython \u5728\u7f16\u8bd1\u4f18\u5316\u65f6, \u67d0\u4e9b\u60c5\u51b5\u4e0b\u4f1a\u5c1d\u8bd5\u4f7f\u7528\u5df2\u7ecf\u5b58\u5728\u7684\u4e0d\u53ef\u53d8\u5bf9\u8c61\u800c\u4e0d\u662f\u6bcf\u6b21\u90fd\u521b\u5efa\u4e00\u4e2a\u65b0\u5bf9\u8c61. (\u8fd9\u79cd\u884c\u4e3a\u88ab\u79f0\u4f5c\u5b57\u7b26\u4e32\u7684\u9a7b\u7559[string interning])\n", "- \u53d1\u751f\u9a7b\u7559\u4e4b\u540e, \u8bb8\u591a\u53d8\u91cf\u53ef\u80fd\u6307\u5411\u5185\u5b58\u4e2d\u7684\u76f8\u540c\u5b57\u7b26\u4e32\u5bf9\u8c61. (\u4ece\u800c\u8282\u7701\u5185\u5b58)\n", "- \u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d, \u5b57\u7b26\u4e32\u662f\u9690\u5f0f\u9a7b\u7559\u7684. \u4f55\u65f6\u53d1\u751f\u9690\u5f0f\u9a7b\u7559\u5219\u53d6\u51b3\u4e8e\u5177\u4f53\u7684\u5b9e\u73b0. \u8fd9\u91cc\u6709\u4e00\u4e9b\u65b9\u6cd5\u53ef\u4ee5\u7528\u6765\u731c\u6d4b\u5b57\u7b26\u4e32\u662f\u5426\u4f1a\u88ab\u9a7b\u7559:\n", " - \u6240\u6709\u957f\u5ea6\u4e3a 0 \u548c\u957f\u5ea6\u4e3a 1 \u7684\u5b57\u7b26\u4e32\u90fd\u88ab\u9a7b\u7559.\n", " - \u5b57\u7b26\u4e32\u5728\u7f16\u8bd1\u65f6\u88ab\u5b9e\u73b0 (`'wtf'` \u5c06\u88ab\u9a7b\u7559, \u4f46\u662f `''.join(['w', 't', 'f'])` \u5c06\u4e0d\u4f1a\u88ab\u9a7b\u7559)\n", " - \u5b57\u7b26\u4e32\u4e2d\u53ea\u5305\u542b\u5b57\u6bcd\uff0c\u6570\u5b57\u6216\u4e0b\u5212\u7ebf\u65f6\u5c06\u4f1a\u9a7b\u7559. \u6240\u4ee5 `'wtf!'` \u7531\u4e8e\u5305\u542b `!` \u800c\u672a\u88ab\u9a7b\u7559. \u53ef\u4ee5\u5728[\u8fd9\u91cc](https://github.com/python/cpython/blob/3.6/Objects/codeobject.c#L19)\u627e\u5230 CPython \u5bf9\u6b64\u89c4\u5219\u7684\u5b9e\u73b0.\n", "\n", " \"\"\n", "\n", "- \u5f53\u5728\u540c\u4e00\u884c\u5c06 `a` \u548c `b` \u7684\u503c\u8bbe\u7f6e\u4e3a `\"wtf!\"` \u7684\u65f6\u5019, Python \u89e3\u91ca\u5668\u4f1a\u521b\u5efa\u4e00\u4e2a\u65b0\u5bf9\u8c61, \u7136\u540e\u540c\u65f6\u5f15\u7528\u7b2c\u4e8c\u4e2a\u53d8\u91cf(\u8bd1: \u4ec5\u9002\u7528\u4e8e3.7\u4ee5\u4e0b, \u8be6\u7ec6\u60c5\u51b5\u8bf7\u770b[\u8fd9\u91cc](https://github.com/leisurelicht/wtfpython-cn/issues/13)). \u5982\u679c\u4f60\u5728\u4e0d\u540c\u7684\u884c\u4e0a\u8fdb\u884c\u8d4b\u503c\u64cd\u4f5c, \u5b83\u5c31\u4e0d\u4f1a\u201c\u77e5\u9053\u201d\u5df2\u7ecf\u6709\u4e00\u4e2a `wtf\uff01` \u5bf9\u8c61 (\u56e0\u4e3a `\"wtf!\"` \u4e0d\u662f\u6309\u7167\u4e0a\u9762\u63d0\u5230\u7684\u65b9\u5f0f\u88ab\u9690\u5f0f\u9a7b\u7559\u7684). \u5b83\u662f\u4e00\u79cd\u7f16\u8bd1\u5668\u4f18\u5316, \u7279\u522b\u9002\u7528\u4e8e\u4ea4\u4e92\u5f0f\u73af\u5883.\n", "- \u5e38\u91cf\u6298\u53e0(constant folding) \u662f Python \u4e2d\u7684\u4e00\u79cd [\u7aa5\u5b54\u4f18\u5316(peephole optimization)](https://en.wikipedia.org/wiki/Peephole_optimization) \u6280\u672f. \u8fd9\u610f\u5473\u7740\u5728\u7f16\u8bd1\u65f6\u8868\u8fbe\u5f0f `'a'*20` \u4f1a\u88ab\u66ff\u6362\u4e3a `'aaaaaaaaaaaaaaaaaaaa'` \u4ee5\u51cf\u5c11\u8fd0\u884c\u65f6\u7684\u65f6\u949f\u5468\u671f. \u53ea\u6709\u957f\u5ea6\u5c0f\u4e8e 20 \u7684\u5b57\u7b26\u4e32\u624d\u4f1a\u53d1\u751f\u5e38\u91cf\u6298\u53e0. (\u4e3a\u5565? \u60f3\u8c61\u4e00\u4e0b\u7531\u4e8e\u8868\u8fbe\u5f0f `'a'*10**10` \u800c\u751f\u6210\u7684 `.pyc` \u6587\u4ef6\u7684\u5927\u5c0f). \u76f8\u5173\u7684\u6e90\u7801\u5b9e\u73b0\u5728[\u8fd9\u91cc](https://github.com/python/cpython/blob/3.6/Python/peephole.c#L288).\n", "- \u5982\u679c\u4f60\u662f\u4f7f\u7528 3.7 \u7248\u672c\u4e2d\u8fd0\u884c\u4e0a\u8ff0\u793a\u4f8b\u4ee3\u7801, \u4f1a\u53d1\u73b0\u90e8\u5206\u4ee3\u7801\u7684\u8fd0\u884c\u7ed3\u679c\u4e0e\u6ce8\u91ca\u8bf4\u660e\u76f8\u540c. \u8fd9\u662f\u56e0\u4e3a\u5728 3.7 \u7248\u672c\u4e2d, \u5e38\u91cf\u6298\u53e0\u5df2\u7ecf\u4ece\u7aa5\u5b54\u4f18\u5316\u5668\u8fc1\u79fb\u81f3\u65b0\u7684 AST \u4f18\u5316\u5668, \u540e\u8005\u53ef\u4ee5\u4ee5\u66f4\u9ad8\u7684\u4e00\u81f4\u6027\u6765\u6267\u884c\u4f18\u5316. (\u7531 Eugene Toder \u548c INADA Naoki \u5728 [bpo-29469](https://bugs.python.org/issue29469) \u548c [bpo-11549](https://bugs.python.org/issue11549) \u4e2d\u8d21\u732e.) \n", "- (\u8bd1: \u4f46\u662f\u5728\u6700\u65b0\u7684 3.8 \u7248\u672c\u4e2d, \u7ed3\u679c\u53c8\u53d8\u56de\u53bb\u4e86. \u867d\u7136 3.8 \u7248\u672c\u548c 3.7 \u7248\u672c\u4e00\u6837, \u90fd\u662f\u4f7f\u7528 AST \u4f18\u5316\u5668. \u76ee\u524d\u4e0d\u786e\u5b9a\u5b98\u65b9\u5bf9 3.8 \u7248\u672c\u7684 AST \u505a\u4e86\u4ec0\u4e48\u8c03\u6574.)\n", "\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### > Be careful with chained operations/\u5c0f\u5fc3\u94fe\u5f0f\u64cd\u4f5c\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "False\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "(False == False) in [False] # \u53ef\u4ee5\u7406\u89e3\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "False\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "False == (False in [False]) # \u53ef\u4ee5\u7406\u89e3\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "True\n", "\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "False == False in [False] # \u4e3a\u6bdb?\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "False\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "True is False == False\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "True\n", "\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "False is False is False\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "True\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "1 > 0 < 1\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "False\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "(1 > 0) < 1\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "False\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "1 > (0 < 1)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### \ud83d\udca1 \u8bf4\u660e:\n", "\n", "\u6839\u636e https://docs.python.org/3/reference/expressions.html#comparisons\n", "\n", "> \u5f62\u5f0f\u4e0a, \u5982\u679c a, b, c, ..., y, z \u662f\u8868\u8fbe\u5f0f, \u800c op1, op2, ..., opN \u662f\u6bd4\u8f83\u8fd0\u7b97\u7b26, \u90a3\u4e48\u9664\u4e86\u6bcf\u4e2a\u8868\u8fbe\u5f0f\u6700\u591a\u53ea\u51fa\u73b0\u4e00\u6b21\u4ee5\u5916 a op1 b op2 c ... y opN z \u5c31\u7b49\u4e8e a op1 b and b op2 c and ... y opN z.\n", "\n", "\u867d\u7136\u4e0a\u9762\u7684\u4f8b\u5b50\u4f3c\u4e4e\u5f88\u611a\u8822, \u4f46\u662f\u50cf `a == b == c` \u6216 `0 <= x <= 100` \u5c31\u5f88\u68d2\u4e86.\n", "\n", "* `False is False is False` \u76f8\u5f53\u4e8e `(False is False) and (False is False)`\n", "* `True is False == False` \u76f8\u5f53\u4e8e `(True is False) and (False == False)`, \u7531\u4e8e\u8bed\u53e5\u7684\u7b2c\u4e00\u90e8\u5206 (`True is False`) \u7b49\u4e8e `False`, \u56e0\u6b64\u6574\u4e2a\u8868\u8fbe\u5f0f\u7684\u7ed3\u679c\u4e3a `False`.\n", "* `1 > 0 < 1` \u76f8\u5f53\u4e8e `(1 > 0) and (0 < 1)`, \u6240\u4ee5\u6700\u7ec8\u7ed3\u679c\u4e3a `True`.\n", "* \u8868\u8fbe\u5f0f `(1 > 0) < 1` \u76f8\u5f53\u4e8e `True < 1` \u4e14\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ " 1\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ " int(True)\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ " 2\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ " True + 1 # \u4e0e\u8fd9\u4e2a\u4f8b\u5b50\u65e0\u5173\uff0c\u53ea\u662f\u5a31\u4e50\u4e00\u4e0b\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " \u6240\u4ee5, `1 < 1` \u7b49\u4e8e `False`\n", "\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### > How not to use `is` operator/\u4e3a\u4ec0\u4e48\u4e0d\u4f7f\u7528 `is` \u64cd\u4f5c\u7b26\n", "\u4e0b\u9762\u662f\u4e00\u4e2a\u5728\u4e92\u8054\u7f51\u4e0a\u975e\u5e38\u6709\u540d\u7684\u4f8b\u5b50.\n", "\n", "1\\.\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "True\n", "\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "a = 256\n", "b = 256\n", "a is b\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "False\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "a = 257\n", "b = 257\n", "a is b\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "2\\.\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "False\n", "\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "a = []\n", "b = []\n", "a is b\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "True\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "a = tuple()\n", "b = tuple()\n", "a is b\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "3\\.\n", "**Output**\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "True\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "a, b = 257, 257\n", "a is b\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "**Output (Python 3.7.x specifically)**\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "False\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "a, b = 257, 257\n", "a is b\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### \ud83d\udca1 \u8bf4\u660e:\n", "\n", "**`is` \u548c `==` \u7684\u533a\u522b**\n", "\n", "* `is` \u8fd0\u7b97\u7b26\u68c0\u67e5\u4e24\u4e2a\u8fd0\u7b97\u5bf9\u8c61\u662f\u5426\u5f15\u7528\u81ea\u540c\u4e00\u5bf9\u8c61 (\u5373, \u5b83\u68c0\u67e5\u4e24\u4e2a\u8fd0\u7b97\u5bf9\u8c61\u662f\u5426\u76f8\u540c).\n", "* `==` \u8fd0\u7b97\u7b26\u6bd4\u8f83\u4e24\u4e2a\u8fd0\u7b97\u5bf9\u8c61\u7684\u503c\u662f\u5426\u76f8\u7b49.\n", "* \u56e0\u6b64 `is` \u4ee3\u8868\u5f15\u7528\u76f8\u540c, `==` \u4ee3\u8868\u503c\u76f8\u7b49. \u4e0b\u9762\u7684\u4f8b\u5b50\u53ef\u4ee5\u5f88\u597d\u7684\u8bf4\u660e\u8fd9\u70b9,\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ " True\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ " [] == []\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ " False\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ " [] is [] # \u8fd9\u4e24\u4e2a\u7a7a\u5217\u8868\u4f4d\u4e8e\u4e0d\u540c\u7684\u5185\u5b58\u5730\u5740.\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "**`256` \u662f\u4e00\u4e2a\u5df2\u7ecf\u5b58\u5728\u7684\u5bf9\u8c61, \u800c `257` \u4e0d\u662f**\n", "\n", "\u5f53\u4f60\u542f\u52a8Python \u7684\u65f6\u5019, \u6570\u503c\u4e3a `-5` \u5230 `256` \u7684\u5bf9\u8c61\u5c31\u5df2\u7ecf\u88ab\u5206\u914d\u597d\u4e86. \u8fd9\u4e9b\u6570\u5b57\u56e0\u4e3a\u7ecf\u5e38\u88ab\u4f7f\u7528, \u6240\u4ee5\u4f1a\u88ab\u63d0\u524d\u51c6\u5907\u597d.\n", "\n", "Python \u901a\u8fc7\u8fd9\u79cd\u521b\u5efa\u5c0f\u6574\u6570\u6c60\u7684\u65b9\u5f0f\u6765\u907f\u514d\u5c0f\u6574\u6570\u9891\u7e41\u7684\u7533\u8bf7\u548c\u9500\u6bc1\u5185\u5b58\u7a7a\u95f4.\n", "\n", "\u5f15\u7528\u81ea https://docs.python.org/3/c-api/long.html\n", "> \u5f53\u524d\u7684\u5b9e\u73b0\u4e3a-5\u5230256\u4e4b\u95f4\u7684\u6240\u6709\u6574\u6570\u4fdd\u7559\u4e00\u4e2a\u6574\u6570\u5bf9\u8c61\u6570\u7ec4, \u5f53\u4f60\u521b\u5efa\u4e86\u4e00\u4e2a\u8be5\u8303\u56f4\u5185\u7684\u6574\u6570\u65f6, \u4f60\u53ea\u9700\u8981\u8fd4\u56de\u73b0\u6709\u5bf9\u8c61\u7684\u5f15\u7528. \u6240\u4ee5\u6539\u53d81\u7684\u503c\u662f\u6709\u53ef\u80fd\u7684. \u6211\u6000\u7591\u8fd9\u79cd\u884c\u4e3a\u5728Python\u4e2d\u662f\u672a\u5b9a\u4e49\u884c\u4e3a. :-)\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "10922528\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "id(256)\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "10922528\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "a = 256\n", "b = 256\n", "id(a)\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "10922528\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "id(b)\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "140084850247312\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "id(257)\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "140084850247440\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "x = 257\n", "y = 257\n", "id(x)\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "140084850247344\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "id(y)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "\u8fd9\u91cc\u89e3\u91ca\u5668\u5e76\u6ca1\u6709\u667a\u80fd\u5230\u80fd\u5728\u6267\u884c `y = 257` \u65f6\u610f\u8bc6\u5230\u6211\u4eec\u5df2\u7ecf\u521b\u5efa\u4e86\u4e00\u4e2a\u6574\u6570 `257`, \u6240\u4ee5\u5b83\u5728\u5185\u5b58\u4e2d\u53c8\u65b0\u5efa\u4e86\u53e6\u4e00\u4e2a\u5bf9\u8c61.\n", "\n", "\u7c7b\u4f3c\u7684\u4f18\u5316\u4e5f\u9002\u7528\u4e8e\u5176\u4ed6**\u4e0d\u53ef\u53d8**\u5bf9\u8c61\uff0c\u4f8b\u5982\u7a7a\u5143\u7ec4\u3002\u7531\u4e8e\u5217\u8868\u662f\u53ef\u53d8\u7684\uff0c\u8fd9\u5c31\u662f\u4e3a\u4ec0\u4e48 `[] is []` \u5c06\u8fd4\u56de `False` \u800c `() is ()` \u5c06\u8fd4\u56de `True`\u3002 \u8fd9\u89e3\u91ca\u4e86\u6211\u4eec\u7684\u7b2c\u4e8c\u4e2a\u4ee3\u7801\u6bb5\u3002\u800c\u7b2c\u4e09\u4e2a\u5462\uff1a\n", "\n", "**\u5f53 `a` \u548c `b` \u5728\u540c\u4e00\u884c\u4e2d\u4f7f\u7528\u76f8\u540c\u7684\u503c\u521d\u59cb\u5316\u65f6\uff0c\u4f1a\u6307\u5411\u540c\u4e00\u4e2a\u5bf9\u8c61.**\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "140640774013296\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "a, b = 257, 257\n", "id(a)\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "140640774013296\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "id(b)\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "140640774013392\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "a = 257\n", "b = 257\n", "id(a)\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "140640774013488\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "id(b)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "* \u5f53 a \u548c b \u5728\u540c\u4e00\u884c\u4e2d\u88ab\u8bbe\u7f6e\u4e3a `257` \u65f6, Python \u89e3\u91ca\u5668\u4f1a\u521b\u5efa\u4e00\u4e2a\u65b0\u5bf9\u8c61, \u7136\u540e\u540c\u65f6\u5f15\u7528\u7b2c\u4e8c\u4e2a\u53d8\u91cf. \u5982\u679c\u4f60\u5728\u4e0d\u540c\u7684\u884c\u4e0a\u8fdb\u884c, \u5b83\u5c31\u4e0d\u4f1a \"\u77e5\u9053\" \u5df2\u7ecf\u5b58\u5728\u4e00\u4e2a `257` \u5bf9\u8c61\u4e86.\n", "* \u8fd9\u662f\u4e00\u79cd\u7279\u522b\u4e3a\u4ea4\u4e92\u5f0f\u73af\u5883\u505a\u7684\u7f16\u8bd1\u5668\u4f18\u5316. \u5f53\u4f60\u5728\u5b9e\u65f6\u89e3\u91ca\u5668\u4e2d\u8f93\u5165\u4e24\u884c\u7684\u65f6\u5019, \u4ed6\u4eec\u4f1a\u5355\u72ec\u7f16\u8bd1, \u56e0\u6b64\u4e5f\u4f1a\u5355\u72ec\u8fdb\u884c\u4f18\u5316. \u5982\u679c\u4f60\u5728 `.py` \u6587\u4ef6\u4e2d\u5c1d\u8bd5\u8fd9\u4e2a\u4f8b\u5b50, \u5219\u4e0d\u4f1a\u770b\u5230\u76f8\u540c\u7684\u884c\u4e3a, \u56e0\u4e3a\u6587\u4ef6\u662f\u4e00\u6b21\u6027\u7f16\u8bd1\u7684\u3002\u8fd9\u79cd\u4f18\u5316\u4e0d\u4ec5\u9650\u4e8e\u6574\u6570\uff0c\u5b83\u4e5f\u9002\u7528\u4e8e\u5176\u4ed6\u4e0d\u53ef\u53d8\u6570\u636e\u7c7b\u578b\uff0c\u4f8b\u5982\u5b57\u7b26\u4e32\uff08\u67e5\u770b\u793a\u4f8b\u201c\u5fae\u5999\u7684\u5b57\u7b26\u4e32\u201d\uff09\u548c\u6d6e\u70b9\u6570\uff0c\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ " True\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ " a, b = 257.0, 257.0\n", " a is b\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "* \u4e3a\u4ec0\u4e48\u8fd9\u4e0d\u9002\u7528\u4e8e Python 3.7\uff1f \u5927\u6982\u662f\u56e0\u4e3a\u6b64\u7c7b\u7f16\u8bd1\u5668\u4f18\u5316\u662f\u7279\u5b9a\u4e8e\u5b9e\u73b0\u7684\uff08\u5373\u53ef\u80fd\u968f\u7248\u672c\u3001\u64cd\u4f5c\u7cfb\u7edf\u7b49\u800c\u53d8\u5316\uff09\u3002\u6211\u4ecd\u5728\u8bd5\u56fe\u5f04\u6e05\u695a\u5bfc\u81f4\u95ee\u9898\u7684\u5177\u4f53\u5b9e\u73b0\u66f4\u6539\uff0c\u60a8\u53ef\u4ee5\u67e5\u770b\u6b64 [\u95ee\u9898](https://github.com/satwikkansal/wtfpython/issues/100) \u4ee5\u83b7\u53d6\u66f4\u65b0\u3002\n", "\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### > Hash brownies/\u662f\u65f6\u5019\u6765\u70b9\u86cb\u7cd5\u4e86!\n", "* hash brownie\u6307\u4e00\u79cd\u542b\u6709\u5927\u9ebb\u6210\u5206\u7684\u86cb\u7cd5, \u6240\u4ee5\u8fd9\u91cc\u662f\u53e5\u53cc\u5173\n", "* \u8fd9\u91cc\u4fdd\u7559\u539f\u4f5c\u8005\u5bf9\u4e8e\u6807\u9898\u7684\u7ffb\u8bd1\n", "\n", "\n", "\n", "1\\.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "some_dict = {}\n", "some_dict[5.5] = \"JavaScript\"\n", "some_dict[5.0] = \"Ruby\"\n", "some_dict[5] = \"Python\"\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "**Output:**\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "\"JavaScript\"\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "some_dict[5.5]\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "\"Python\"\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "some_dict[5.0] # \"Python\" \u6d88\u9664\u4e86 \"Ruby\" \u7684\u5b58\u5728?\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "\"Python\"\n", "\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "some_dict[5] \n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "complex\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "complex_five = 5 + 0j\n", "type(complex_five)\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "\"Python\"\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "some_dict[complex_five]\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "\u4e3a\u4ec0\u4e48\u5230\u5904\u90fd\u662fPython?\n", "\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### \ud83d\udca1 \u8bf4\u660e\n", "\n", "\n", "* \u8fd9\u4e2a StackOverflow\u7684 [\u56de\u7b54](https://stackoverflow.com/a/32211042/4354153) \u6f02\u4eae\u5730\u89e3\u91ca\u4e86\u8fd9\u80cc\u540e\u7684\u57fa\u672c\u539f\u7406.\n", "\n", "* Python \u5b57\u5178\u4e2d\u952e\u7684\u552f\u4e00\u6027\u662f\u6839\u636e *\u7b49\u4ef7\u6027*\uff0c\u800c\u4e0d\u662f\u540c\u4e00\u6027\u3002 \u56e0\u6b64\uff0c\u5373\u4f7f `5`\u3001`5.0` \u548c `5 + 0j` \u662f\u4e0d\u540c\u7c7b\u578b\u7684\u4e0d\u540c\u5bf9\u8c61\uff0c\u7531\u4e8e\u5b83\u4eec\u662f\u76f8\u7b49\u7684\uff0c\u5b83\u4eec\u4e0d\u80fd\u90fd\u5728\u540c\u4e00\u4e2a `dict`\uff08\u6216 `set`\uff09\u4e2d\u3002 \u53ea\u8981\u60a8\u63d2\u5165\u5176\u4e2d\u4efb\u4f55\u4e00\u4e2a\uff0c\u5c1d\u8bd5\u67e5\u627e\u4efb\u4f55\u4e0d\u540c\u4f46\u7b49\u4ef7\u7684\u952e\u90fd\u5c06\u4f7f\u7528\u539f\u59cb\u6620\u5c04\u503c\u6210\u529f\uff08\u800c\u4e0d\u662f\u56e0\u201cKeyError\u201d\u800c\u5931\u8d25\uff09\uff1a\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ " True\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ " 5 == 5.0 == 5 + 0j\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ " True\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ " 5 is not 5.0 is not 5 + 0j\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ " True\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ " some_dict = {}\n", " some_dict[5.0] = \"Ruby\"\n", " 5.0 in some_dict\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ " True\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ " (5 in some_dict) and (5 + 0j in some_dict)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "* \u8fd9\u5728\u8d4b\u503c\u7684\u65f6\u5019\u4e5f\u4f1a\u751f\u6548\u3002\u56e0\u6b64\uff0c\u5f53\u60a8\u6267\u884c `some_dict[5] = \"Python\"` \u65f6\uff0cPython \u4f1a\u627e\u5230\u5177\u6709\u7b49\u4ef7\u952e\u503c `5.0 -> \"Ruby\"` \u7684\u73b0\u6709\u9879\uff0c\u8986\u76d6\u5176\u503c\uff0c\u5e76\u4fdd\u7559\u539f\u59cb\u952e\u503c\u3002\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ " {5.0: 'Ruby'}\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ " some_dict\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ " {5.0: 'Python'}\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ " some_dict[5] = \"Python\"\n", " some_dict\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "* \u90a3\u4e48\u6211\u4eec\u5982\u4f55\u5c06\u952e\u503c\u66f4\u65b0\u4e3a`5`\uff08\u800c\u4e0d\u662f`5.0`\uff09\uff1f \u6211\u4eec\u5b9e\u9645\u4e0a\u4e0d\u80fd\u539f\u5730\u66f4\u65b0\uff0c\u4f46\u662f\u6211\u4eec\u53ef\u4ee5\u5148\u5220\u9664\u952e\uff08`del some_dict[5.0]`\uff09\uff0c\u7136\u540e\u91cd\u65b0\u8d4b\u503c\uff08`some_dict[5]`\uff09\u5f97\u5230\u6574\u6570`5` \u4f5c\u4e3a\u952e\u800c\u4e0d\u662f\u6d6e\u70b9\u6570 `5.0`\uff0c\u5c3d\u7ba1\u8fd9\u5c5e\u4e8e\u6781\u5c11\u6570\u60c5\u51b5\u3002\n", "\n", "* Python \u662f\u5982\u4f55\u5728\u5305\u542b `5.0` \u7684\u5b57\u5178\u4e2d\u627e\u5230 `5` \u7684\uff1f Python \u53ea\u9700\u8981\u82b1\u8d39\u5e38\u6570\u65f6\u95f4\uff0c\u800c\u65e0\u9700\u4f7f\u7528\u54c8\u5e0c\u51fd\u6570\u904d\u5386\u6bcf\u4e00\u9879\u3002\u5f53 Python \u5728 dict \u4e2d\u67e5\u627e\u952e `foo` \u65f6\uff0c\u5b83\u9996\u5148\u8ba1\u7b97 `hash(foo)`\uff08\u4ee5\u5e38\u6570\u65f6\u95f4\u8fd0\u884c\uff09\u3002\u56e0\u4e3a\u5728 Python \u4e2d\uff0c\u8981\u6c42\u76f8\u7b49\u7684\u5bf9\u8c61\u5177\u6709\u76f8\u540c\u7684\u54c8\u5e0c\u503c\uff08\u6b64\u5904\u4e3a[\u6587\u6863](https://docs.python.org/3/reference/datamodel.html#object.__hash__)\uff09\uff0c`5` \u3001`5.0` \u548c `5 + 0j` \u5177\u6709\u76f8\u540c\u7684\u54c8\u5e0c\u503c\u3002\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ " True\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ " 5 == 5.0 == 5 + 0j\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ " True\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ " hash(5) == hash(5.0) == hash(5 + 0j)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", " **\u6ce8\u610f\uff1a** \u53cd\u4e4b\u4e0d\u4e00\u5b9a\u6b63\u786e\uff1a\u5177\u6709\u76f8\u7b49\u54c8\u5e0c\u503c\u7684\u5bf9\u8c61\u672c\u8eab\u53ef\u80fd\u4e0d\u76f8\u7b49\u3002\uff08\u8fd9\u662f[\u54c8\u5e0c\u51b2\u7a81](https://en.wikipedia.org/wiki/Collision_(computer_science)\uff09\u9020\u6210\u7684\uff0c\u8fd9\u4e5f\u4f1a\u964d\u4f4e\u54c8\u5e0c\u8fd0\u7b97\u7684\u6027\u80fd\u3002\uff09\n", "\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### > Deep down, we're all the same./\u672c\u8d28\u4e0a,\u6211\u4eec\u90fd\u4e00\u6837. *\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "class WTF:\n", " pass\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "**Output:**\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "False\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "WTF() == WTF() # \u4e24\u4e2a\u4e0d\u540c\u7684\u5bf9\u8c61\u5e94\u8be5\u4e0d\u76f8\u7b49\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "False\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "WTF() is WTF() # \u4e5f\u4e0d\u76f8\u540c\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "True\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "hash(WTF()) == hash(WTF()) # \u54c8\u5e0c\u503c\u4e5f\u5e94\u8be5\u4e0d\u540c\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "True\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "id(WTF()) == id(WTF())\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### \ud83d\udca1 \u8bf4\u660e:\n", "\n", "* \u5f53\u8c03\u7528 `id` \u51fd\u6570\u65f6, Python \u521b\u5efa\u4e86\u4e00\u4e2a `WTF` \u7c7b\u7684\u5bf9\u8c61\u5e76\u4f20\u7ed9 `id` \u51fd\u6570. \u7136\u540e `id` \u51fd\u6570\u83b7\u53d6\u5176id\u503c (\u4e5f\u5c31\u662f\u5185\u5b58\u5730\u5740), \u7136\u540e\u4e22\u5f03\u8be5\u5bf9\u8c61. \u8be5\u5bf9\u8c61\u5c31\u88ab\u9500\u6bc1\u4e86.\n", "* \u5f53\u6211\u4eec\u8fde\u7eed\u4e24\u6b21\u8fdb\u884c\u8fd9\u4e2a\u64cd\u4f5c\u65f6, Python\u4f1a\u5c06\u76f8\u540c\u7684\u5185\u5b58\u5730\u5740\u5206\u914d\u7ed9\u7b2c\u4e8c\u4e2a\u5bf9\u8c61. \u56e0\u4e3a (\u5728CPython\u4e2d) `id` \u51fd\u6570\u4f7f\u7528\u5bf9\u8c61\u7684\u5185\u5b58\u5730\u5740\u4f5c\u4e3a\u5bf9\u8c61\u7684id\u503c, \u6240\u4ee5\u4e24\u4e2a\u5bf9\u8c61\u7684id\u503c\u662f\u76f8\u540c\u7684.\n", "* \u7efc\u4e0a, \u5bf9\u8c61\u7684id\u503c\u4ec5\u4ec5\u5728\u5bf9\u8c61\u7684\u751f\u547d\u5468\u671f\u5185\u552f\u4e00. \u5728\u5bf9\u8c61\u88ab\u9500\u6bc1\u4e4b\u540e, \u6216\u88ab\u521b\u5efa\u4e4b\u524d, \u5176\u4ed6\u5bf9\u8c61\u53ef\u4ee5\u5177\u6709\u76f8\u540c\u7684id\u503c.\n", "* \u90a3\u4e3a\u4ec0\u4e48 `is` \u64cd\u4f5c\u7684\u7ed3\u679c\u4e3a `False` \u5462? \u8ba9\u6211\u4eec\u770b\u770b\u8fd9\u6bb5\u4ee3\u7801.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ " class WTF(object):\n", " def __init__(self): print(\"I\")\n", " def __del__(self): print(\"D\")\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", " **Output:**\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ " I\n", " I\n", " D\n", " D\n", " False\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ " WTF() is WTF()\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ " I\n", " D\n", " I\n", " D\n", " True\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ " id(WTF()) == id(WTF())\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " \u6b63\u5982\u4f60\u6240\u770b\u5230\u7684, \u5bf9\u8c61\u9500\u6bc1\u7684\u987a\u5e8f\u662f\u9020\u6210\u6240\u6709\u4e0d\u540c\u4e4b\u5904\u7684\u539f\u56e0.\n", "\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### > Disorder within order/\u6709\u5e8f\u4e2d\u6f5c\u85cf\u7740\u65e0\u5e8f *\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "from collections import OrderedDict\n", "\n", "dictionary = dict()\n", "dictionary[1] = 'a'; dictionary[2] = 'b';\n", "\n", "ordered_dict = OrderedDict()\n", "ordered_dict[1] = 'a'; ordered_dict[2] = 'b';\n", "\n", "another_ordered_dict = OrderedDict()\n", "another_ordered_dict[2] = 'b'; another_ordered_dict[1] = 'a';\n", "\n", "class DictWithHash(dict):\n", " \"\"\"\n", " \u5b9e\u73b0\u4e86 __hash__ \u9b54\u6cd5\u65b9\u6cd5\u7684dict\u7c7b\n", " \"\"\"\n", " __hash__ = lambda self: 0\n", "\n", "class OrderedDictWithHash(OrderedDict):\n", " \"\"\"\n", " \u5b9e\u73b0\u4e86 __hash__ \u9b54\u6cd5\u65b9\u6cd5\u7684OrderedDict\u7c7b\n", " \"\"\"\n", " __hash__ = lambda self: 0\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "**Output**\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "True\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "dictionary == ordered_dict # \u5982\u679c a == b\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "True\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "dictionary == another_ordered_dict # \u4e14 b == c\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "False\n", "\n", "# \u4f17\u6240\u5468\u77e5\uff0cset\u6570\u636e\u7ed3\u6784\u50a8\u5b58\u4e0d\u91cd\u590d\u5143\u7d20\uff0c\n", "# \u8ba9\u6211\u4eec\u751f\u6210\u4ee5\u4e0a\u5b57\u5178\u7684 set \u6570\u636e\u7c7b\u578b\uff0c\u770b\u770b\u4f1a\u53d1\u751f\u4ec0\u4e48\u2026\u2026\n", "\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "ordered_dict == another_ordered_dict # \u90a3\u4e48\u4e3a\u4ec0\u4e48 c == a \u4e0d\u6210\u7acb??\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "Traceback (most recent call last):\n", " File \"\", line 1, in \n", "TypeError: unhashable type: 'dict'\n", "\n", "# dict\u7c7b\u6ca1\u6709\u5b9e\u73b0 __hash__ \uff0c\u51fa\u9519\u53ef\u4ee5\u7406\u89e3\uff0c\u63a5\u4e0b\u6765\u4f7f\u7528\u6211\u4eec\u6d3e\u751f\u7684\u7c7b\u3002\n", "\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "len({dictionary, ordered_dict, another_ordered_dict})\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "1\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "dictionary = DictWithHash()\n", "dictionary[1] = 'a'; dictionary[2] = 'b';\n", "ordered_dict = OrderedDictWithHash()\n", "ordered_dict[1] = 'a'; ordered_dict[2] = 'b';\n", "another_ordered_dict = OrderedDictWithHash()\n", "another_ordered_dict[2] = 'b'; another_ordered_dict[1] = 'a';\n", "len({dictionary, ordered_dict, another_ordered_dict})\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "2\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "len({ordered_dict, another_ordered_dict, dictionary}) # \u4ea4\u6362\u987a\u5e8f\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "\u5230\u5e95\u53d1\u751f\u4e86\u4ec0\u4e48?\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### \ud83d\udca1 \u8bf4\u660e:\n", "\n", "- \u7b49\u53f7\u7684\u4f20\u9012\u6027\u6ca1\u6709\u5728 `dictionary`, `ordered_dict` \u548c `another_ordered_dict` \u4e4b\u95f4\u751f\u6548\u662f `OrderedDict` \u7c7b\u4e2d `__eq__` \u65b9\u6cd5\u7684\u5b9e\u73b0\u65b9\u5f0f\u9020\u6210\u7684\u3002\u6839\u636e[\u6587\u6863](https://docs.python.org/3/library/collections.html#ordereddict-objects)\u4ee5\u4e0b\u90e8\u5206:\n", " \n", " > \u5bf9\u4e8e `OrderedDict` \u7c7b\u4e4b\u95f4\uff0c\u76f8\u7b49\u6027\u7684\u5224\u5b9a\u662f\u4f4d\u7f6e\u654f\u611f\u7684\uff0c\u5b9e\u73b0\u7c7b\u4f3c\u4e8e `list(od1.items())==list(od2.items())`\u3002\u5bf9\u4e8e `OrderedDict` \u7c7b\u4e0e\u5176\u4ed6 `Mapping` \u5bf9\u8c61\uff08\u4f8b\u5982`dict` \u7c7b\uff09\uff0c\u76f8\u7b49\u6027\u7684\u5224\u5b9a\u662f\u975e\u4f4d\u7f6e\u654f\u611f\u7684\u3002\n", "- \u8fd9\u662f\u4e3a\u4e86\u4efb\u4f55\u4f7f\u7528\u5e38\u89c4 `dict` \u7c7b\u7684\u5730\u65b9\u80fd\u591f\u76f4\u63a5\u4f7f\u7528 `OrderedDict` \u5bf9\u8c61\u4ee3\u66ff\u3002\n", "- \u597d\u5566\uff0c\u90a3\u4e3a\u4ec0\u4e48\u6539\u53d8\u987a\u5e8f\u4f1a\u5f71\u54cd `set` \u5bf9\u8c61\u751f\u6210\u7684\u957f\u5ea6\u5462? \u7b54\u6848\u5c31\u662f\u4e0a\u9762\u8bf4\u7684\u7f3a\u4e4f\u7b49\u53f7\u7684\u4f20\u9012\u6027\u3002\u56e0\u4e3a `set` \u7c7b\u662f\u552f\u4e00\u5143\u7d20\u7684\u65e0\u5e8f\u96c6\u5408\uff0c\u5143\u7d20\u63d2\u5165\u7684\u987a\u5e8f\u4e0d\u5e94\u8be5\u6709\u5f71\u54cd\u3002\u4f46\u5728\u6b64\u4f8b\u4e2d\uff0c\u786e\u6709\u4e0d\u540c\u3002\u8ba9\u6211\u4eec\u8fdb\u4e00\u6b65\u6df1\u5165\u3002\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ " True\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ " some_set = set()\n", " some_set.add(dictionary) # \u6d89\u53ca\u7684\u53d8\u91cf\u662f\u524d\u5e8f\u7247\u6bb5\u5b9a\u4e49\u7684 mapping \u5bf9\u8c61\n", " ordered_dict in some_set\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ " 1\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ " some_set.add(ordered_dict)\n", " len(some_set)\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ " True\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ " another_ordered_dict in some_set\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ " 1\n", "\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ " some_set.add(another_ordered_dict)\n", " len(some_set)\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ " False\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ " another_set = set()\n", " another_set.add(ordered_dict)\n", " another_ordered_dict in another_set\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ " 2\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ " another_set.add(another_ordered_dict)\n", " len(another_set)\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ " True\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ " dictionary in another_set\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ " 2\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ " another_set.add(another_ordered_dict)\n", " len(another_set)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " \u56e0\u6b64\uff0c\u4e0d\u4e00\u81f4\u6027\u662f\u7531\u4e8e `another_ordered_dict in another_set` \u7ed3\u679c\u4e3a `False`\u3002 \u56e0\u4e3a `ordered_dict` \u5df2\u7ecf\u5728 `another_set` \u4e2d\uff0c\u4f46\u5982\u524d\u6240\u8ff0\uff0c `ordered_dict == another_ordered_dict` \u7684\u7ed3\u679c\u4e3a `False`\uff0c\u4f1a\u5728\u540e\u7eed\u518d\u52a0\u5165 `another_ordered_dict` \u5230 `another_set` \u4e2d\u3002\n", "\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### > Keep trying.../\u4e0d\u505c\u7684try *\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "def some_func():\n", " try:\n", " return 'from_try'\n", " finally:\n", " return 'from_finally'\n", "\n", "def another_func(): \n", " for _ in range(3):\n", " try:\n", " continue\n", " finally:\n", " print(\"Finally!\")\n", "\n", "def one_more_func(): # A gotcha!\n", " try:\n", " for i in range(3):\n", " try:\n", " 1 / i\n", " except ZeroDivisionError:\n", " # Let's throw it here and handle it outside for loop\n", " raise ZeroDivisionError(\"A trivial divide by zero error\")\n", " finally:\n", " print(\"Iteration\", i)\n", " break\n", " except ZeroDivisionError as e:\n", " print(\"Zero division error occurred\", e)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "**Output:**\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "'from_finally'\n", "\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "some_func()\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "Finally!\n", "Finally!\n", "Finally!\n", "\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "another_func()\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "Traceback (most recent call last):\n", " File \"\", line 1, in \n", "ZeroDivisionError: division by zero\n", "\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "1 / 0\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "Iteration 0\n", "\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "one_more_func()\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### \ud83d\udca1 \u8bf4\u660e:\n", "\n", "- \u5f53\u5728 \"try...finally\" \u8bed\u53e5\u7684 `try` \u4e2d\u6267\u884c `return`, `break` \u6216 `continue` \u540e, `finally` \u5b50\u53e5\u4f9d\u7136\u4f1a\u6267\u884c.\n", "- \u51fd\u6570\u7684\u8fd4\u56de\u503c\u7531\u6700\u540e\u6267\u884c\u7684 `return` \u8bed\u53e5\u51b3\u5b9a. \u7531\u4e8e `finally` \u5b50\u53e5\u4e00\u5b9a\u4f1a\u6267\u884c, \u6240\u4ee5 `finally` \u5b50\u53e5\u4e2d\u7684 `return` \u5c06\u59cb\u7ec8\u662f\u6700\u540e\u6267\u884c\u7684\u8bed\u53e5.\n", "- \u8fd9\u91cc\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u5982\u679c finally \u5b50\u53e5\u6267\u884c `return` \u6216 `break` \u8bed\u53e5\uff0c\u4e34\u65f6\u4fdd\u5b58\u7684\u5f02\u5e38\u5c06\u88ab\u4e22\u5f03\u3002\n", "\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### > For what?/\u4e3a\u4ec0\u4e48?\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "some_string = \"wtf\"\n", "some_dict = {}\n", "for i, some_dict[i] in enumerate(some_string):\n", " pass\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "**Output:**\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "{0: 'w', 1: 't', 2: 'f'}\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "some_dict # \u521b\u5efa\u4e86\u7d22\u5f15\u5b57\u5178.\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### \ud83d\udca1 \u8bf4\u660e:\n", "\n", "* [Python \u8bed\u6cd5](https://docs.python.org/3/reference/grammar.html) \u4e2d\u5bf9 `for` \u7684\u5b9a\u4e49\u662f:\n", " ```\n", " for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite]\n", " ```\n", " \u5176\u4e2d `exprlist` \u6307\u5206\u914d\u76ee\u6807. \u8fd9\u610f\u5473\u7740\u5bf9\u53ef\u8fed\u4ee3\u5bf9\u8c61\u4e2d\u7684**\u6bcf\u4e00\u9879\u90fd\u4f1a\u6267\u884c**\u7c7b\u4f3c `{exprlist} = {next_value}` \u7684\u64cd\u4f5c.\n", "\n", " \u4e00\u4e2a\u6709\u8da3\u7684\u4f8b\u5b50\u8bf4\u660e\u4e86\u8fd9\u4e00\u70b9:\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ " for i in range(4):\n", " print(i)\n", " i = 10\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", " **Output:**\n", " ```\n", " 0\n", " 1\n", " 2\n", " 3\n", " ```\n", "\n", " \u4f60\u53ef\u66fe\u89c9\u5f97\u8fd9\u4e2a\u5faa\u73af\u53ea\u4f1a\u8fd0\u884c\u4e00\u6b21?\n", "\n", " **\ud83d\udca1 \u8bf4\u660e:**\n", "\n", " - \u7531\u4e8e\u5faa\u73af\u5728Python\u4e2d\u5de5\u4f5c\u65b9\u5f0f, \u8d4b\u503c\u8bed\u53e5 `i = 10` \u5e76\u4e0d\u4f1a\u5f71\u54cd\u8fed\u4ee3\u5faa\u73af, \u5728\u6bcf\u6b21\u8fed\u4ee3\u5f00\u59cb\u4e4b\u524d, \u8fed\u4ee3\u5668(\u8fd9\u91cc\u6307 `range(4)`) \u751f\u6210\u7684\u4e0b\u4e00\u4e2a\u5143\u7d20\u5c31\u88ab\u89e3\u5305\u5e76\u8d4b\u503c\u7ed9\u76ee\u6807\u5217\u8868\u7684\u53d8\u91cf(\u8fd9\u91cc\u6307 `i`)\u4e86.\n", "\n", "* \u5728\u6bcf\u4e00\u6b21\u7684\u8fed\u4ee3\u4e2d, `enumerate(some_string)` \u51fd\u6570\u5c31\u751f\u6210\u4e00\u4e2a\u65b0\u503c `i` (\u8ba1\u6570\u5668\u589e\u52a0) \u5e76\u4ece `some_string` \u4e2d\u83b7\u53d6\u4e00\u4e2a\u5b57\u7b26. \u7136\u540e\u5c06\u5b57\u5178 `some_dict` \u952e `i` (\u521a\u521a\u5206\u914d\u7684) \u7684\u503c\u8bbe\u4e3a\u8be5\u5b57\u7b26. \u672c\u4f8b\u4e2d\u5faa\u73af\u7684\u5c55\u5f00\u53ef\u4ee5\u7b80\u5316\u4e3a:\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ " i, some_dict[i] = (0, 'w')\n", " i, some_dict[i] = (1, 't')\n", " i, some_dict[i] = (2, 'f')\n", " some_dict\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### > Evaluation time discrepancy/\u6267\u884c\u65f6\u673a\u5dee\u5f02\n", "1\\.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "array = [1, 8, 15]\n", "# \u4e00\u4e2a\u5178\u578b\u7684\u751f\u6210\u5668\u8868\u8fbe\u5f0f\n", "g = (x for x in array if array.count(x) > 0)\n", "array = [2, 8, 22]\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "**Output:**\n" ] }, { "cell_type": "code", "metadata": { "collapsed": true }, "execution_count": null, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[8]\n" ] } ], "source": [ "print(list(g)) #\u5176\u4ed6\u7684\u503c\u53bb\u54ea\u513f\u4e86\uff1f\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "2\\.\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "array_1 = [1,2,3,4]\n", "g1 = (x for x in array_1)\n", "array_1 = [1,2,3,4,5]\n", "\n", "array_2 = [1,2,3,4]\n", "g2 = (x for x in array_2)\n", "array_2[:] = [1,2,3,4,5]\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "**Output:**\n" ] }, { "cell_type": "code", "metadata": { "collapsed": true }, "execution_count": null, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1,2,3,4]\n", "\n" ] } ], "source": [ "print(list(g1))\n" ] }, { "cell_type": "code", "metadata": { "collapsed": true }, "execution_count": null, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1,2,3,4,5]\n" ] } ], "source": [ "print(list(g2))\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "3\\.\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "array_3 = [1, 2, 3]\n", "array_4 = [10, 20, 30]\n", "gen = (i + j for i in array_3 for j in array_4)\n", "\n", "array_3 = [4, 5, 6]\n", "array_4 = [400, 500, 600]\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "**Output:**\n" ] }, { "cell_type": "code", "metadata": { "collapsed": true }, "execution_count": null, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[401, 501, 601, 402, 502, 602, 403, 503, 603]\n" ] } ], "source": [ "print(list(gen))\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### \ud83d\udca1 \u8bf4\u660e\n", "\n", "- \u5728[\u751f\u6210\u5668](https://wiki.python.org/moin/Generators)\u8868\u8fbe\u5f0f\u4e2d, `in` \u5b50\u53e5\u5728\u58f0\u660e\u65f6\u6267\u884c, \u800c\u6761\u4ef6\u5b50\u53e5\u5219\u662f\u5728\u8fd0\u884c\u65f6\u6267\u884c.\n", "- \u6240\u4ee5\u5728\u8fd0\u884c\u524d, `array` \u5df2\u7ecf\u88ab\u91cd\u65b0\u8d4b\u503c\u4e3a `[2, 8, 22]`, \u56e0\u6b64\u5bf9\u4e8e\u4e4b\u524d\u7684 `1`, `8` \u548c `15`, \u53ea\u6709 `count(8)` \u7684\u7ed3\u679c\u662f\u5927\u4e8e `0` \u7684, \u6240\u4ee5\u751f\u6210\u5668\u53ea\u4f1a\u751f\u6210 `8`.\n", "- \u7b2c\u4e8c\u90e8\u5206\u4e2d `g1` \u548c `g2` \u7684\u8f93\u51fa\u5dee\u5f02\u5219\u662f\u7531\u4e8e\u53d8\u91cf `array_1` \u548c `array_2` \u88ab\u91cd\u65b0\u8d4b\u503c\u7684\u65b9\u5f0f\u5bfc\u81f4\u7684.\n", "- \u5728\u7b2c\u4e00\u79cd\u60c5\u51b5\u4e0b, `array_1` \u88ab\u7ed1\u5b9a\u5230\u65b0\u5bf9\u8c61 `[1,2,3,4,5]`, \u56e0\u4e3a `in` \u5b50\u53e5\u662f\u5728\u58f0\u660e\u65f6\u88ab\u6267\u884c\u7684\uff0c \u6240\u4ee5\u5b83\u4ecd\u7136\u5f15\u7528\u65e7\u5bf9\u8c61 `[1,2,3,4]`(\u5e76\u6ca1\u6709\u88ab\u9500\u6bc1).\n", "- \u5728\u7b2c\u4e8c\u79cd\u60c5\u51b5\u4e0b, \u5bf9 `array_2` \u7684\u5207\u7247\u8d4b\u503c\u5c06\u76f8\u540c\u7684\u65e7\u5bf9\u8c61 `[1,2,3,4]` \u539f\u5730\u66f4\u65b0\u4e3a `[1,2,3,4,5]`. \u56e0\u6b64 `g2` \u548c `array_2` \u4ecd\u7136\u5f15\u7528\u540c\u4e00\u4e2a\u5bf9\u8c61(\u8fd9\u4e2a\u5bf9\u8c61\u73b0\u5728\u5df2\u7ecf\u66f4\u65b0\u4e3a `[1,2,3,4,5]`).\n", "- \u597d\u5566\uff0c\u6309\u7167\u76ee\u524d\u8ba8\u8bba\u7684\u903b\u8f91\uff0c\u7b2c\u4e09\u4e2a\u4ee3\u7801\u6bb5\u4e2d\u7684 `list(gen)` \u7684\u503c\u4e0d\u5e94\u8be5\u662f `[11, 21, 31, 12, 22, 32, 13, 23, 33]` \u5417? \uff08\u6bd5\u7adf `array_3` \u548c `array_4` \u7684\u884c\u4e3a\u4e0e `array_1` \u4e00\u6837\uff09\u3002 [PEP-289](https://www.python.org/dev/peps/pep-0289/#the-details) \u4e2d\u89e3\u91ca\u4e86\uff08\u53ea\u6709\uff09`array_4` \u503c\u66f4\u65b0\u7684\u539f\u56e0\n", " > \u53ea\u6709\u6700\u5916\u5c42\u7684 for \u8868\u8fbe\u5f0f\u4f1a\u7acb\u5373\u8ba1\u7b97\uff0c\u5176\u4ed6\u8868\u8fbe\u5f0f\u4f1a\u5ef6\u8fdf\u5230\u751f\u6210\u5668\u8fd0\u884c\u3002\n", "\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### > `is not ...` is not `is (not ...)`/`is not ...` \u4e0d\u662f `is (not ...)`\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "True\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "'something' is not None\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "False\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "'something' is (not None)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### \ud83d\udca1 \u8bf4\u660e:\n", "\n", "- `is not` \u662f\u4e2a\u5355\u72ec\u7684\u4e8c\u5143\u8fd0\u7b97\u7b26, \u4e0e\u5206\u522b\u4f7f\u7528 `is` \u548c `not` \u4e0d\u540c.\n", "- \u5982\u679c\u64cd\u4f5c\u7b26\u4e24\u4fa7\u7684\u53d8\u91cf\u6307\u5411\u540c\u4e00\u4e2a\u5bf9\u8c61, \u5219 `is not` \u7684\u7ed3\u679c\u4e3a `False`, \u5426\u5219\u7ed3\u679c\u4e3a `True`.\n", "\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### > A tic-tac-toe where X wins in the first attempt!/\u4e00\u8e74\u5373\u81f3!\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "# \u6211\u4eec\u5148\u521d\u59cb\u5316\u4e00\u4e2a\u53d8\u91cfrow\n", "row = [\"\"]*3 #row i['', '', '']\n", "# \u5e76\u521b\u5efa\u4e00\u4e2a\u53d8\u91cfboard\n", "board = [row]*3\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "**Output:**\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "[['', '', ''], ['', '', ''], ['', '', '']]\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "board\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "['', '', '']\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "board[0]\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "''\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "board[0][0]\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "[['X', '', ''], ['X', '', ''], ['X', '', '']]\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "board[0][0] = \"X\"\n", "board\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "\u6211\u4eec\u6709\u6ca1\u6709\u8d4b\u503c\u8fc73\u4e2a \"X\" \u5462\uff1f\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### \ud83d\udca1 \u8bf4\u660e:\n", "\n", "\u5f53\u6211\u4eec\u521d\u59cb\u5316 `row` \u53d8\u91cf\u65f6, \u4e0b\u9762\u8fd9\u5f20\u56fe\u5c55\u793a\u4e86\u5185\u5b58\u4e2d\u7684\u60c5\u51b5\u3002\n", "\n", "![image](/images/tic-tac-toe/after_row_initialized.png)\n", "\n", "\u800c\u5f53\u901a\u8fc7\u5bf9 `row` \u505a\u4e58\u6cd5\u6765\u521d\u59cb\u5316 `board` \u65f6, \u5185\u5b58\u4e2d\u7684\u60c5\u51b5\u5219\u5982\u4e0b\u56fe\u6240\u793a (\u6bcf\u4e2a\u5143\u7d20 `board[0]`, `board[1]` \u548c `board[2]` \u90fd\u548c `row` \u4e00\u6837\u5f15\u7528\u4e86\u540c\u4e00\u5217\u8868.)\n", "\n", "![image](/images/tic-tac-toe/after_board_initialized.png)\n", "\n", "\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u4e0d\u4f7f\u7528\u53d8\u91cf `row` \u751f\u6210 `board` \u6765\u907f\u514d\u8fd9\u79cd\u60c5\u51b5. ([\u8fd9\u4e2a](https://github.com/satwikkansal/wtfpython/issues/68)issue\u63d0\u51fa\u4e86\u8fd9\u4e2a\u9700\u6c42.)\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "[['X', '', ''], ['', '', ''], ['', '', '']]\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "board = [['']*3 for _ in range(3)]\n", "board[0][0] = \"X\"\n", "board\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### > Schr\u00f6dinger's variable/\u859b\u5b9a\u8c14\u7684\u53d8\u91cf *\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "funcs = []\n", "results = []\n", "for x in range(7):\n", " def some_func():\n", " return x\n", " funcs.append(some_func)\n", " results.append(some_func()) # \u6ce8\u610f\u8fd9\u91cc\u51fd\u6570\u88ab\u6267\u884c\u4e86\n", "\n", "funcs_results = [func() for func in funcs]\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "**Output:**\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "[0, 1, 2, 3, 4, 5, 6]\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "results\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "[6, 6, 6, 6, 6, 6, 6]\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "funcs_results\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "\u5373\u4f7f\u6bcf\u6b21\u5728\u8fed\u4ee3\u4e2d\u5c06 `some_func` \u52a0\u5165 `funcs` \u524d\u7684 `x` \u503c\u90fd\u4e0d\u76f8\u540c, \u6240\u6709\u7684\u51fd\u6570\u8fd8\u662f\u90fd\u8fd4\u56de6.\n", "\n", "// \u518d\u6362\u4e2a\u4f8b\u5b50\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "[512, 512, 512, 512, 512, 512, 512, 512, 512, 512]\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "powers_of_x = [lambda x: x**i for i in range(10)]\n", "[f(2) for f in powers_of_x]\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### \ud83d\udca1 \u8bf4\u660e:\n", "\n", "- \u5f53\u5728\u5faa\u73af\u5185\u90e8\u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570\u65f6, \u5982\u679c\u8be5\u51fd\u6570\u5728\u5176\u4e3b\u4f53\u4e2d\u4f7f\u7528\u4e86\u5faa\u73af\u53d8\u91cf, \u5219\u95ed\u5305\u51fd\u6570\u5c06\u4e0e\u5faa\u73af\u53d8\u91cf\u7ed1\u5b9a, \u800c\u4e0d\u662f\u5b83\u7684\u503c. \u56e0\u6b64, \u6240\u6709\u7684\u51fd\u6570\u90fd\u662f\u4f7f\u7528\u6700\u540e\u5206\u914d\u7ed9\u53d8\u91cf\u7684\u503c\u6765\u8fdb\u884c\u8ba1\u7b97\u7684.\n", "\n", "- \u53ef\u4ee5\u901a\u8fc7\u5c06\u5faa\u73af\u53d8\u91cf\u4f5c\u4e3a\u547d\u540d\u53d8\u91cf\u4f20\u9012\u7ed9\u51fd\u6570\u6765\u83b7\u5f97\u9884\u671f\u7684\u7ed3\u679c. **\u4e3a\u4ec0\u4e48\u8fd9\u6837\u53ef\u884c?** \u56e0\u4e3a\u8fd9\u4f1a\u5728\u51fd\u6570\u5185\u518d\u6b21\u5b9a\u4e49\u4e00\u4e2a\u5c40\u90e8\u53d8\u91cf\u3002\u6211\u4eec\u53ef\u4ee5\u770b\u5230\u5b83\u4f7f\u7528\u4e86\u6765\u81ea\u4e0a\u4e0b\u6587\u7684`x`\uff08\u5373*\u4e0d\u662f*\u5c40\u90e8\u53d8\u91cf\uff09\uff1a\n", "\uff08\u8bd1\u8005\u6ce8: inspect\u4f4d\u4e8ePython\u6807\u51c6\u5e93\u4e2d\uff0c\u8be5\u6a21\u5757\u7528\u4e8e\u6536\u96c6python\u5bf9\u8c61\u7684\u4fe1\u606f\uff0c\u53ef\u4ee5\u83b7\u53d6\u7c7b\u6216\u51fd\u6570\u7684\u53c2\u6570\u7684\u4fe1\u606f\uff0c\u6e90\u7801\uff0c\u89e3\u6790\u5806\u6808\uff0c\u5bf9\u5bf9\u8c61\u8fdb\u884c\u7c7b\u578b\u68c0\u67e5\u7b49\u7b49\uff0cPython3.3+\u7248\u672c\u652f\u6301getclosurevars\u51fd\u6570\uff09\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "ClosureVars(nonlocals={}, globals={'x': 6}, builtins={}, unbound=set())\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "import inspect\n", "inspect.getclosurevars(funcs[0])\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "\u7531\u4e8e `x` \u662f\u4e00\u4e2a\u5168\u5c40\u503c\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u66f4\u65b0 `x` \u6765\u66f4\u6539 `funcs` \u7528\u6765\u67e5\u627e\u548c\u8fd4\u56de\u7684\u503c\uff1a\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "[42, 42, 42, 42, 42, 42, 42]\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "x = 42\n", "[func() for func in funcs]\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "* \u8981\u83b7\u5f97\u60f3\u8981\u7684\u7ed3\u679c\uff0c\u60a8\u53ef\u4ee5\u5c06\u5faa\u73af\u53d8\u91cf\u4f5c\u4e3a\u547d\u540d\u53d8\u91cf\u4f20\u9012\u7ed9\u51fd\u6570\u3002 **\u4e3a\u4ec0\u4e48\u4f1a\u8fd9\u6837\uff1f** \u56e0\u4e3a\u8fd9\u4f1a\u5728\u51fd\u6570\u7684\u4f5c\u7528\u57df\u5185\u5b9a\u4e49\u53d8\u91cf\u3002 \u5b83\u5c06\u4e0d\u518d\u8fdb\u5165\u5468\u56f4\uff08\u5168\u5c40\uff09\u8303\u56f4\u6765\u67e5\u627e\u53d8\u91cf\u503c\uff0c\u800c\u662f\u4f1a\u521b\u5efa\u4e00\u4e2a\u5c40\u90e8\u53d8\u91cf\u6765\u5b58\u50a8\u8be5\u65f6\u95f4\u70b9\u7684\u201cx\u201d\u503c\u3002\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "funcs = []\n", "for x in range(7):\n", " def some_func(x=x):\n", " return x\n", " funcs.append(some_func)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "**Output:**\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "[0, 1, 2, 3, 4, 5, 6]\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "funcs_results = [func() for func in funcs]\n", "funcs_results\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "\u6b64\u65f6\uff0c\u4e0d\u518d\u4f7f\u7528\u5168\u5c40\u53d8\u91cf `x`\uff1a\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "ClosureVars(nonlocals={}, globals={}, builtins={}, unbound=set())\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "inspect.getclosurevars(funcs[0])\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### > The chicken-egg problem/\u5148\u6709\u9e21\u8fd8\u662f\u5148\u6709\u86cb *\n", "1\\.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "True\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "isinstance(3, int)\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "True\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "isinstance(type, object)\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "True\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "isinstance(object, type)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\u90a3\u4e48\u5230\u5e95\u8c01\u662f\u201c\u6700\u7ec8\u201d\u7684\u57fa\u7c7b\u5462\uff1f\u4e0b\u8fb9\u987a\u4fbf\u5217\u51fa\u66f4\u591a\u7684\u4ee4\u4eba\u56f0\u60d1\u7684\u5730\u65b9\n", "\n", "2\\. \n", "\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "False\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "class A: pass\n", "isinstance(A, A)\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "True\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "isinstance(type, type)\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "True\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "isinstance(object, object)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "3\\.\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "True\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "issubclass(int, object)\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "True\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "issubclass(type, object)\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "False\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "issubclass(object, type)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### \ud83d\udca1 \u8bf4\u660e\n", "\n", "- `type` \u662f Python \u4e2d\u7684[\u5143\u7c7b](https://realpython.com/python-metaclasses/)\u3002\n", "- Python \u4e2d\uff0c**\u4e00\u5207**\u7686\u5bf9\u8c61\uff0c\u5176\u4e2d\u5305\u62ec\u7c7b\u53ca\u5176\u5bf9\u8c61\uff08\u5b9e\u4f8b\uff09\u3002\n", "- `type` \u7c7b\u578b\u662f`object`\u7c7b\u7684\u5143\u7c7b\uff0c\u6bcf\u4e2a\u7c7b\uff08\u5305\u62ec`type`\uff09\u90fd\u76f4\u63a5\u6216\u95f4\u63a5\u5730\u7ee7\u627f\u81ea`object`\u3002\n", "- \u5bf9\u8c61\u548c\u7c7b\u578b\u4e4b\u95f4\u6ca1\u6709\u771f\u6b63\u7684\u57fa\u7c7b\u3002\u4e0a\u8ff0\u7247\u6bb5\u4e2d\u7684\u4ee4\u4eba\u56f0\u60d1\u7684\u5730\u65b9\u4e4b\u6240\u4ee5\u51fa\u73b0\uff0c\u662f\u56e0\u4e3a\u6211\u4eec\u4ece Python \u7c7b\u7684\u89d2\u5ea6\u8003\u8651\u8fd9\u4e9b\u5173\u7cfb\uff08issubclass \u548c isinstance\uff09\u3002 `object`\u548c`type`\u4e4b\u95f4\u7684\u5173\u7cfb\u4e0d\u80fd\u5728\u7eafpython\u4e2d\u91cd\u73b0\u3002 \u66f4\u51c6\u786e\u5730\u8bf4\uff0c\u4ee5\u4e0b\u5173\u7cfb\u4e0d\u80fd\u5728\u7eaf Python \u4e2d\u91cd\u73b0:\n", " + A\u7c7b\u662fB\u7c7b\u7684\u4e00\u4e2a\u5b9e\u4f8b\uff0cB\u7c7b\u662fA\u7c7b\u7684\u4e00\u4e2a\u5b9e\u4f8b\u3002\n", " + A\u7c7b\u662f\u5b83\u81ea\u5df1\u7684\u4e00\u4e2a\u5b9e\u4f8b\u3002\n", "- `object`\u548c`type`\u4e4b\u95f4\u7684\u5173\u7cfb\uff08\u65e2\u662f\u5f7c\u6b64\u7684\u5b9e\u4f8b\uff0c\u4e5f\u662f\u5b83\u4eec\u81ea\u5df1\u7684\u5b9e\u4f8b\uff09\u5b58\u5728\u4e8e Python \u4e2d\uff0c\u8fd9\u662f\u6e90\u4e8e\u5b9e\u73b0\u5c42\u7ea7\u4e0a\u7684\u201c\u4f5c\u5f0a\u201d\u884c\u4e3a\u3002\n", "\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### > Subclass relationships/\u5b50\u7c7b\u5173\u7cfb *\n", "**Output:**\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "True\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "from collections import Hashable\n", "issubclass(list, object)\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "True\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "issubclass(object, Hashable)\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "False\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "issubclass(list, Hashable)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "\u5b50\u7c7b\u5173\u7cfb\u5e94\u8be5\u662f\u53ef\u4f20\u9012\u7684, \u5bf9\u5427? (\u5373, \u5982\u679c `A` \u662f `B` \u7684\u5b50\u7c7b, `B` \u662f `C` \u7684\u5b50\u7c7b, \u90a3\u4e48 `A` _\u5e94\u8be5_ \u662f `C` \u7684\u5b50\u7c7b.)\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### \ud83d\udca1 \u8bf4\u660e:\n", "\n", "* Python \u4e2d\u7684\u5b50\u7c7b\u5173\u7cfb\u5e76\u4e0d\u4e00\u5b9a\u662f\u4f20\u9012\u7684. \u4efb\u4f55\u4eba\u90fd\u53ef\u4ee5\u5728\u5143\u7c7b\u4e2d\u968f\u610f\u5b9a\u4e49 `__subclasscheck__`.\n", "* \u5f53 `issubclass(cls, Hashable)` \u88ab\u8c03\u7528\u65f6, \u5b83\u53ea\u662f\u5728 `cls` \u4e2d\u5bfb\u627e `__hash__` \u65b9\u6cd5\u6216\u8005\u4ece\u7ee7\u627f\u7684\u7236\u7c7b\u4e2d\u5bfb\u627e `__hash__` \u65b9\u6cd5.\n", "* \u7531\u4e8e `object` is \u53ef\u6563\u5217\u7684(hashable), \u4f46\u662f `list` \u662f\u4e0d\u53ef\u6563\u5217\u7684, \u6240\u4ee5\u5b83\u6253\u7834\u4e86\u8fd9\u79cd\u4f20\u9012\u5173\u7cfb.\n", "* \u5728[\u8fd9\u91cc](https://www.naftaliharris.com/blog/python-subclass-intransitivity/)\u53ef\u4ee5\u627e\u5230\u66f4\u8be6\u7ec6\u7684\u89e3\u91ca.\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### > Methods equality and identity/\u65b9\u6cd5\u7684\u76f8\u7b49\u6027\u548c\u552f\u4e00\u6027 *\n", "1.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "class SomeClass:\n", " def method(self):\n", " pass\n", "\n", " @classmethod\n", " def classm(cls):\n", " pass\n", "\n", " @staticmethod\n", " def staticm():\n", " pass\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "**Output:**\n" ] }, { "cell_type": "code", "metadata": { "collapsed": true }, "execution_count": null, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "True\n" ] } ], "source": [ "print(SomeClass.method is SomeClass.method)\n" ] }, { "cell_type": "code", "metadata": { "collapsed": true }, "execution_count": null, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "False\n" ] } ], "source": [ "print(SomeClass.classm is SomeClass.classm)\n" ] }, { "cell_type": "code", "metadata": { "collapsed": true }, "execution_count": null, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "True\n" ] } ], "source": [ "print(SomeClass.classm == SomeClass.classm)\n" ] }, { "cell_type": "code", "metadata": { "collapsed": true }, "execution_count": null, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "True\n" ] } ], "source": [ "print(SomeClass.staticm is SomeClass.staticm)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\u8bbf\u95ee `classm` \u4e24\u6b21\uff0c\u6211\u4eec\u5f97\u5230\u4e00\u4e2a\u76f8\u7b49\u7684\u5bf9\u8c61\uff0c\u4f46\u4e0d\u662f*\u540c\u4e00\u4e2a*\uff1f \u8ba9\u6211\u4eec\u770b\u770b `SomeClass` \u7684\u5b9e\u4f8b\u4f1a\u53d1\u751f\u4ec0\u4e48\uff1a\n", "\n", "2.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "o1 = SomeClass()\n", "o2 = SomeClass()\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "**Output:**\n" ] }, { "cell_type": "code", "metadata": { "collapsed": true }, "execution_count": null, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "False\n" ] } ], "source": [ "print(o1.method == o2.method)\n" ] }, { "cell_type": "code", "metadata": { "collapsed": true }, "execution_count": null, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "True\n" ] } ], "source": [ "print(o1.method == o1.method)\n" ] }, { "cell_type": "code", "metadata": { "collapsed": true }, "execution_count": null, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "False\n" ] } ], "source": [ "print(o1.method is o1.method)\n" ] }, { "cell_type": "code", "metadata": { "collapsed": true }, "execution_count": null, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "False\n" ] } ], "source": [ "print(o1.classm is o1.classm)\n" ] }, { "cell_type": "code", "metadata": { "collapsed": true }, "execution_count": null, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "True\n" ] } ], "source": [ "print(o1.classm == o1.classm == o2.classm == SomeClass.classm)\n" ] }, { "cell_type": "code", "metadata": { "collapsed": true }, "execution_count": null, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "True\n" ] } ], "source": [ "print(o1.staticm is o1.staticm is o2.staticm is SomeClass.staticm)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "\u8bbf\u95ee ` classm` or `method` \u4e24\u6b21, \u4e3a `SomeClass` \u7684\u540c\u4e00\u4e2a\u5b9e\u4f8b\u521b\u5efa\u4e86\u76f8\u7b49\u4f46\u662f*\u4e0d\u540c*\u7684\u5bf9\u8c61\u3002\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### \ud83d\udca1 \u8bf4\u660e\n", "* \u51fd\u6570\u662f[\u63cf\u8ff0\u7b26](https://docs.python.org/3/howto/descriptor.html)\u3002\u6bcf\u5f53\u5c06\u51fd\u6570\u4f5c\u4e3a\u5c5e\u6027\u8bbf\u95ee\u65f6\uff0c\u5c31\u4f1a\u8c03\u7528\u63cf\u8ff0\u7b26\uff0c\u521b\u5efa\u4e00\u4e2a\u65b9\u6cd5\u5bf9\u8c61\uff0c\u8be5\u5bf9\u8c61\u5c06\u51fd\u6570\u4e0e\u62e5\u6709\u8be5\u5c5e\u6027\u7684\u5bf9\u8c61\u201c\u7ed1\u5b9a\u201d\u3002\u5982\u679c\u88ab\u8c03\u7528\uff0c\u8be5\u65b9\u6cd5\u8c03\u7528\u51fd\u6570\uff0c\u9690\u5f0f\u4f20\u9012\u7ed1\u5b9a\u5bf9\u8c61\u4f5c\u4e3a\u7b2c\u4e00\u4e2a\u53c2\u6570\uff08\u8fd9\u5c31\u662f\u6211\u4eec\u5982\u4f55\u5c06 self \u4f5c\u4e3a\u7b2c\u4e00\u4e2a\u53c2\u6570\u83b7\u53d6\uff0c\u5c3d\u7ba1\u6ca1\u6709\u663e\u5f0f\u4f20\u9012\u5b83\uff09\u3002\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ ">\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "o1.method\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "* \u591a\u6b21\u8bbf\u95ee\u8be5\u5c5e\u6027\uff0c\u6bcf\u6b21\u90fd\u4f1a\u521b\u5efa\u4e00\u4e2a\u65b9\u6cd5\u5bf9\u8c61\uff01 \u56e0\u6b64\uff0c`o1.method is o1.method` \u6c38\u8fdc\u4e0d\u4f1a\u662f\u771f\u7684\u3002\u4f46\u662f\uff0c\u5c06\u51fd\u6570\u4f5c\u4e3a\u7c7b\u5c5e\u6027\uff08\u800c\u4e0d\u662f\u5b9e\u4f8b\uff09\u8bbf\u95ee\u5e76\u4e0d\u4f1a\u521b\u5efa\u65b9\u6cd5\u5bf9\u8c61\uff0c\u6240\u4ee5 `SomeClass.method is SomeClass.method` \u662f\u771f\u7684\u3002\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "SomeClass.method\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "* `classmethod` \u5c06\u51fd\u6570\u8f6c\u6362\u4e3a\u7c7b\u65b9\u6cd5\u3002 \u7c7b\u65b9\u6cd5\u662f\u63cf\u8ff0\u7b26\uff0c\u5f53\u88ab\u8bbf\u95ee\u65f6\uff0c\u5b83\u4f1a\u521b\u5efa\u4e00\u4e2a\u7ed1\u5b9a*\u7c7b\u672c\u8eab*\u7684\u65b9\u6cd5\u5bf9\u8c61\uff0c\u800c\u4e0d\u662f\u5bf9\u8c61\u672c\u8eab\u3002\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ ">\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "o1.classm\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "* \u4e0e\u51fd\u6570\u4e0d\u540c\uff0c`classmethod` \u5728\u4f5c\u4e3a\u7c7b\u5c5e\u6027\u8bbf\u95ee\u65f6\u4e5f\u4f1a\u521b\u5efa\u4e00\u4e2a\u65b9\u6cd5\uff08\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u5b83\u4eec\u7ed1\u5b9a\u7c7b\uff0c\u800c\u4e0d\u662f\u7c7b\u7684\u7c7b\u578b\uff09\u3002 \u6240\u4ee5 `SomeClass.classm is SomeClass.classm` \u662f\u5047\u7684\u3002\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ ">\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "SomeClass.classm\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "* \u5f53\u4e24\u4e2a\u51fd\u6570\u76f8\u7b49\u5e76\u4e14\u7ed1\u5b9a\u7684\u5bf9\u8c61\u76f8\u540c\u65f6\uff0c\u65b9\u6cd5\u5bf9\u8c61\u6bd4\u8f83\u76f8\u7b49\u3002 \u6240\u4ee5`o1.method == o1.method` \u4e3a\u771f\uff0c\u5c3d\u7ba1\u5b83\u4eec\u5728\u5185\u5b58\u4e2d\u662f\u4e24\u4e2a\u4e0d\u540c\u7684\u5bf9\u8c61\u3002\n", "* `staticmethod` \u5c06\u51fd\u6570\u8f6c\u6362\u4e3a\u201c\u65e0\u64cd\u4f5c\u201d\u63cf\u8ff0\u7b26\uff0c\u5b83\u6309\u539f\u6837\u8fd4\u56de\u51fd\u6570\u3002\u6ca1\u6709\u65b9\u6cd5\u5bf9\u8c61\u88ab\u521b\u5efa\uff0c\u6240\u4ee5 `is` \u7684\u6bd4\u8f83\u8fd0\u7b97\u4e3a\u771f\u3002\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "o1.staticm\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "SomeClass.staticm\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "* \u6bcf\u6b21 Python \u8c03\u7528\u5b9e\u4f8b\u65b9\u6cd5\u65f6\u90fd\u5fc5\u987b\u521b\u5efa\u65b0\u7684\u201c\u65b9\u6cd5\u201d\u5bf9\u8c61\uff0c\u5e76\u4e14\u6bcf\u6b21\u90fd\u5fc5\u987b\u4fee\u6539\u53c2\u6570\u4ee5\u63d2\u5165 `self` \u4e25\u91cd\u5f71\u54cd\u6027\u80fd\u3002CPython 3.7 [\u89e3\u51b3\u4e86\u8fd9\u4e2a\u95ee\u9898](https://bugs.python.org/issue26110) \u3002\u901a\u8fc7\u5f15\u5165\u65b0\u7684\u64cd\u4f5c\u7801\u6765\u5904\u7406\u8c03\u7528\u65b9\u6cd5\u800c\u4e0d\u521b\u5efa\u4e34\u65f6\u65b9\u6cd5\u5bf9\u8c61\u3002\u8fd9\u4ec5\u5728\u5b9e\u9645\u8c03\u7528\u8bbf\u95ee\u7684\u51fd\u6570\u65f6\u4f7f\u7528\uff0c\u56e0\u6b64\u8fd9\u91cc\u7684\u4ee3\u7801\u7247\u6bb5\u4e0d\u53d7\u5f71\u54cd\uff0c\u4ecd\u7136\u4f1a\u751f\u6210\u65b9\u6cd5:)\n", "\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### > All-true-ation/\u8fd4\u56deTrue\u7684all\u51fd\u6570 *\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "True\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "all([True, True, True])\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "False\n", "\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "all([True, True, False])\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "True\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "all([])\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "False\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "all([[]])\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "True\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "all([[[]]])\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "\u4e3a\u4ec0\u4e48\u4f1a\u6709\u8fd9\u79cdTrue-False\u7684\u53d8\u5316\uff1f\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### \ud83d\udca1 \u8bf4\u660e\n", "\n", "- `all` \u51fd\u6570\u7684\u5b9e\u73b0\u7b49\u4ef7\u4e8e\uff1a\n", "\n", "- ```py\n", " def all(iterable):\n", " for element in iterable:\n", " if not element:\n", " return False\n", " return True\n", " ```\n", "\n", "- `all([])` \u8fd4\u56de `True` \u56e0\u4e3a\u53ef\u8fed\u4ee3\u5bf9\u8c61\u4e3a\u7a7a\u3002 \n", "- `all([[]])` \u8fd4\u56de `False` \u56e0\u4e3a\u4f20\u5165\u7684\u6570\u7ec4\u6709\u4e00\u4e2a\u5143\u7d20 `[]`\uff0c \u5728Python\u4e2d\uff0c\u7a7a\u5217\u8868\u4e3a\u5047\u3002\n", "- `all([[[]]])` \u548c\u66f4\u9ad8\u7684\u9012\u5f52\u53d8\u4f53\u603b\u662f`True`\u3002 \u8fd9\u662f\u56e0\u4e3a\u4f20\u9012\u7684\u6570\u7ec4\u7684\u5355\u4e2a\u5143\u7d20\uff08`[[...]]`\uff09\u4e0d\u518d\u662f\u7a7a\u7684\uff0c\u800c\u6709\u503c\u7684\u5217\u8868\u4e3a\u771f\u3002\n", "\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### > The surprising comma/\u610f\u5916\u7684\u9017\u53f7\n", "**Output:**\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ " File \"\", line 1\n", " def h(x, **kwargs,):\n", " ^\n", "SyntaxError: invalid syntax\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "def f(x, y,):\n", " print(x, y)\n", "\n", "def g(x=4, y=5,):\n", " print(x, y)\n", "\n", "def h(x, **kwargs,):\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ " File \"\", line 1\n", " def h(*args,):\n", " ^\n", "SyntaxError: invalid syntax\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "def h(*args,):\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### \ud83d\udca1 \u8bf4\u660e:\n", "\n", "- \u5728Python\u51fd\u6570\u7684\u5f62\u5f0f\u53c2\u6570\u5217\u8868\u4e2d, \u5c3e\u968f\u9017\u53f7\u5e76\u4e0d\u4e00\u5b9a\u662f\u5408\u6cd5\u7684.\n", "- \u5728Python\u4e2d, \u53c2\u6570\u5217\u8868\u90e8\u5206\u7528\u524d\u7f6e\u9017\u53f7\u5b9a\u4e49, \u90e8\u5206\u7528\u5c3e\u968f\u9017\u53f7\u5b9a\u4e49. \u8fd9\u79cd\u51b2\u7a81\u5bfc\u81f4\u9017\u53f7\u88ab\u5939\u5728\u4e2d\u95f4, \u6ca1\u6709\u89c4\u5219\u5b9a\u4e49\u5b83.(\u8bd1:\u8fd9\u4e00\u53e5\u770b\u5f97\u6211\u4e5f\u5f88\u61f5\u903c,\u53ea\u80fd\u5f3a\u7ffb\u4e86.\u8be6\u7ec6\u89e3\u91ca\u770b\u4e0b\u9762\u7684\u8ba8\u8bba\u5e16\u4f1a\u4e00\u76ee\u4e86\u7136.)\n", "- **\u6ce8\u610f:** \u5c3e\u968f\u9017\u53f7\u7684\u95ee\u9898\u5df2\u7ecf\u5728Python 3.6\u4e2d\u88ab[\u4fee\u590d](https://bugs.python.org/issue9232)\u4e86. \u800c\u8fd9\u7bc7[\u5e16\u5b50](https://bugs.python.org/issue9232#msg248399)\u4e2d\u5219\u7b80\u8981\u8ba8\u8bba\u4e86Python\u4e2d\u5c3e\u968f\u9017\u53f7\u7684\u4e0d\u540c\u7528\u6cd5.\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### > Strings and the backslashes/\u5b57\u7b26\u4e32\u4e0e\u53cd\u659c\u6760\n", "**Output:**\n" ] }, { "cell_type": "code", "metadata": { "collapsed": true }, "execution_count": null, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\"\n", "\n" ] } ], "source": [ "print(\"\\\"\")\n" ] }, { "cell_type": "code", "metadata": { "collapsed": true }, "execution_count": null, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\\\"\n", "\n" ] } ], "source": [ "print(r\"\\\"\")\n" ] }, { "cell_type": "code", "metadata": { "collapsed": true }, "execution_count": null, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "File \"\", line 1\n", " print(r\"\\\")\n", " ^\n", "SyntaxError: EOL while scanning string literal\n", "\n" ] } ], "source": [ "print(r\"\\\")\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "True\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "r'\\'' == \"\\\\'\"\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### \ud83d\udca1 \u8bf4\u660e:\n", "\n", "- \u5728\u4e00\u822c\u7684python\u5b57\u7b26\u4e32\u4e2d\uff0c\u53cd\u659c\u6760\u7528\u4e8e\u8f6c\u4e49\u53ef\u80fd\u5177\u6709\u7279\u6b8a\u542b\u4e49\u7684\u5b57\u7b26\uff08\u5982\u5355\u5f15\u53f7\u3001\u53cc\u5f15\u53f7\u548c\u53cd\u659c\u6760\u672c\u8eab\uff09\u3002\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ " 'wt\"f'\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ " \"wt\\\"f\"\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "- \u5728\u4ee5 `r` \u5f00\u5934\u7684\u539f\u59cb\u5b57\u7b26\u4e32\u4e2d, \u53cd\u659c\u6760\u5e76\u6ca1\u6709\u7279\u6b8a\u542b\u4e49.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ " True\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ " r'wt\\\"f' == 'wt\\\\\"f'\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ " 'wt\\\\\"f'\n", "\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ " print(repr(r'wt\\\"f')\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ " print(\"\\n\")\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ " '\\\\n'\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ " print(r\"\\\\n\")\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "- \u8fd9\u610f\u5473\u7740\u5f53\u89e3\u6790\u5668\u5728\u539f\u59cb\u5b57\u7b26\u4e32\u4e2d\u9047\u5230\u53cd\u659c\u6760\u65f6\uff0c\u5b83\u671f\u671b\u540e\u9762\u6709\u53e6\u4e00\u4e2a\u5b57\u7b26\u3002 \u5728\u6211\u4eec\u7684\u4f8b\u5b50\u4e2d\uff08`print(r\"\\\")`\uff09\uff0c\u53cd\u659c\u6760\u8f6c\u4e49\u4e86\u5c3e\u968f\u5f15\u53f7\uff0c\u4f7f\u89e3\u6790\u5668\u6ca1\u6709\u7ec8\u6b62\u5f15\u53f7\uff08\u56e0\u6b64\u4ea7\u751f\u4e86`SyntaxError`\uff09\u3002 \u8fd9\u5c31\u662f\u4e3a\u4ec0\u4e48\u53cd\u659c\u6760\u5728\u539f\u59cb\u5b57\u7b26\u4e32\u672b\u5c3e\u4e0d\u8d77\u4f5c\u7528\u7684\u539f\u56e0\u3002\n", "\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### > not knot!/\u522b\u7ea0\u7ed3!\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "x = True\n", "y = False\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "**Output:**\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "True\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "not x == y\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ " File \"\", line 1\n", " x == not y\n", " ^\n", "SyntaxError: invalid syntax\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "x == not y\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### \ud83d\udca1 \u8bf4\u660e:\n", "\n", "* \u8fd0\u7b97\u7b26\u7684\u4f18\u5148\u7ea7\u4f1a\u5f71\u54cd\u8868\u8fbe\u5f0f\u7684\u6c42\u503c\u987a\u5e8f, \u800c\u5728 Python \u4e2d `==` \u8fd0\u7b97\u7b26\u7684\u4f18\u5148\u7ea7\u8981\u9ad8\u4e8e `not` \u8fd0\u7b97\u7b26.\n", "* \u6240\u4ee5 `not x == y` \u76f8\u5f53\u4e8e `not (x == y)`, \u540c\u65f6\u7b49\u4ef7\u4e8e `not (True == False)`, \u6700\u540e\u7684\u8fd0\u7b97\u7ed3\u679c\u5c31\u662f `True`.\n", "* \u4e4b\u6240\u4ee5 `x == not y` \u4f1a\u629b\u4e00\u4e2a `SyntaxError` \u5f02\u5e38, \u662f\u56e0\u4e3a\u5b83\u4f1a\u88ab\u8ba4\u4e3a\u7b49\u4ef7\u4e8e `(x == not) y`, \u800c\u4e0d\u662f\u4f60\u4e00\u5f00\u59cb\u671f\u671b\u7684 `x == (not y)`.\n", "* \u89e3\u91ca\u5668\u671f\u671b `not` \u6807\u8bb0\u662f `not in` \u64cd\u4f5c\u7b26\u7684\u4e00\u90e8\u5206 (\u56e0\u4e3a `==` \u548c `not in` \u64cd\u4f5c\u7b26\u5177\u6709\u76f8\u540c\u7684\u4f18\u5148\u7ea7), \u4f46\u662f\u5b83\u5728 `not` \u6807\u8bb0\u540e\u9762\u627e\u4e0d\u5230 `in` \u6807\u8bb0, \u6240\u4ee5\u4f1a\u629b\u51fa `SyntaxError` \u5f02\u5e38.\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### > Half triple-quoted strings/\u4e09\u4e2a\u5f15\u53f7\n", "**Output:**\n" ] }, { "cell_type": "code", "metadata": { "collapsed": true }, "execution_count": null, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "wtfpython\n" ] } ], "source": [ "print('wtfpython''')\n" ] }, { "cell_type": "code", "metadata": { "collapsed": true }, "execution_count": null, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "wtfpython\n" ] } ], "source": [ "print(\"wtfpython\"\"\")\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "# \u4e0b\u9762\u7684\u8bed\u53e5\u4f1a\u629b\u51fa `SyntaxError` \u5f02\u5e38\n", "# print('''wtfpython')\n", "# print(\"\"\"wtfpython\")\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### \ud83d\udca1 \u8bf4\u660e:\n", "+ Python \u63d0\u4f9b\u9690\u5f0f\u7684[\u5b57\u7b26\u4e32\u8fde\u63a5](https://docs.python.org/2/reference/lexical_analysis.html#string-literal-concatenation), \u4f8b\u5982,\n", " ```\n", " >>> print(\"wtf\" \"python\")\n", " wtfpython\n", " >>> print(\"wtf\" \"\") # or \"wtf\"\"\"\n", " wtf\n", " ```\n", "+ `'''` \u548c `\"\"\"` \u5728 Python\u4e2d\u4e5f\u662f\u5b57\u7b26\u4e32\u5b9a\u754c\u7b26, Python \u89e3\u91ca\u5668\u5728\u5148\u9047\u5230\u4e09\u4e2a\u5f15\u53f7\u7684\u7684\u65f6\u5019\u4f1a\u5c1d\u8bd5\u518d\u5bfb\u627e\u4e09\u4e2a\u7ec8\u6b62\u5f15\u53f7\u4f5c\u4e3a\u5b9a\u754c\u7b26, \u5982\u679c\u4e0d\u5b58\u5728\u5219\u4f1a\u5bfc\u81f4 `SyntaxError` \u5f02\u5e38.\n", "\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### > What's wrong with booleans?/\u5e03\u5c14\u4f60\u548b\u4e86?\n", "1\\.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "# \u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50, \u7edf\u8ba1\u4e0b\u9762\u53ef\u8fed\u4ee3\u5bf9\u8c61\u4e2d\u7684\u5e03\u5c14\u578b\u503c\u7684\u4e2a\u6570\u548c\u6574\u578b\u503c\u7684\u4e2a\u6570\n", "mixed_list = [False, 1.0, \"some_string\", 3, True, [], False]\n", "integers_found_so_far = 0\n", "booleans_found_so_far = 0\n", "\n", "for item in mixed_list:\n", " if isinstance(item, int):\n", " integers_found_so_far += 1\n", " elif isinstance(item, bool):\n", " booleans_found_so_far += 1\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "**Output:**\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "4\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "integers_found_so_far\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "0\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "booleans_found_so_far\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "2\\.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "'wtf'\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "some_bool = True\n", "\"wtf\" * some_bool\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "''\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "some_bool = False\n", "\"wtf\" * some_bool\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "3\\.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "def tell_truth():\n", " True = False\n", " if True == False:\n", " print(\"I have lost faith in truth!\")\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "**Output (< 3.x):**\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "I have lost faith in truth!\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "tell_truth()\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### \ud83d\udca1 \u8bf4\u660e:\n", "\n", "* \u5e03\u5c14\u503c\u662f `int` \u7684\u5b50\u7c7b\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ " True\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ " issubclass(bool, int)\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ " False\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ " issubclass(int, bool)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "* \u56e0\u6b64\uff0c`True` \u548c `False` \u662f `int` \u7684\u5b9e\u4f8b\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ " True\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ " isinstance(True, int)\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ " True\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ " isinstance(False, int)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "* `True` \u7684\u6574\u6570\u503c\u662f `1`, \u800c `False` \u7684\u6574\u6570\u503c\u662f `0`\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ " 1\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ " int(True)\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ " 0\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ " int(False)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "* \u5173\u4e8e\u5176\u80cc\u540e\u7684\u539f\u7406, \u8bf7\u770b\u8fd9\u4e2a StackOverflow \u7684[\u56de\u7b54](https://stackoverflow.com/a/8169049/4354153).\n", "\n", "* \u6700\u521d\uff0cPython \u6ca1\u6709 `bool` \u7c7b\u578b\uff08\u4eba\u4eec\u4f7f\u7528 0 \u8868\u793a\u5047\uff0c\u4f7f\u7528\u975e\u96f6\u503c\uff0c\u5982 1 \u8868\u793a\u771f\uff09\u3002`True`\u3001`False` \u548c `bool` \u7c7b\u578b\u5728 2.x \u7248\u672c\u4e2d\u88ab\u6dfb\u52a0\uff0c\u4f46\u4e3a\u4e86\u5411\u540e\u517c\u5bb9\uff0c`True` \u548c `False` \u4e0d\u80fd\u6210\u4e3a\u5e38\u91cf\u3002\u5b83\u4eec\u53ea\u662f\u5185\u7f6e\u53d8\u91cf\uff0c\u53ef\u4ee5\u91cd\u65b0\u5206\u914d\u5b83\u4eec\n", "\n", "* Python 3 \u5411\u540e\u4e0d\u517c\u5bb9\uff0c\u95ee\u9898\u7ec8\u4e8e\u5f97\u5230\u89e3\u51b3\uff0c\u56e0\u6b64\u6700\u540e\u4e00\u4e2a\u4ee3\u7801\u6bb5\u4e0d\u9002\u7528\u4e8e Python 3.x\uff01\n", "\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### > Class attributes and instance attributes/\u7c7b\u5c5e\u6027\u548c\u5b9e\u4f8b\u5c5e\u6027\n", "1\\.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "class A:\n", " x = 1\n", "\n", "class B(A):\n", " pass\n", "\n", "class C(A):\n", " pass\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "**Output:**\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "(1, 1, 1)\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "A.x, B.x, C.x\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "(1, 2, 1)\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "B.x = 2\n", "A.x, B.x, C.x\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "(3, 2, 3)\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "A.x = 3\n", "A.x, B.x, C.x\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "(3, 3)\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "a = A()\n", "a.x, A.x\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "(4, 3)\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "a.x += 1\n", "a.x, A.x\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "2\\.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "class SomeClass:\n", " some_var = 15\n", " some_list = [5]\n", " another_list = [5]\n", " def __init__(self, x):\n", " self.some_var = x + 1\n", " self.some_list = self.some_list + [x]\n", " self.another_list += [x]\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "**Output:**\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "[5, 420]\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "some_obj = SomeClass(420)\n", "some_obj.some_list\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "[5, 420]\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "some_obj.another_list\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "[5, 111]\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "another_obj = SomeClass(111)\n", "another_obj.some_list\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "[5, 420, 111]\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "another_obj.another_list\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "True\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "another_obj.another_list is SomeClass.another_list\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "True\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "another_obj.another_list is some_obj.another_list\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### \ud83d\udca1 \u8bf4\u660e:\n", "\n", "* \u7c7b\u53d8\u91cf\u548c\u5b9e\u4f8b\u53d8\u91cf\u5728\u5185\u90e8\u662f\u901a\u8fc7\u7c7b\u5bf9\u8c61\u7684\u5b57\u5178\u6765\u5904\u7406(\u8bd1: \u5c31\u662f `__dict__` \u5c5e\u6027). \u5982\u679c\u5728\u5f53\u524d\u7c7b\u7684\u5b57\u5178\u4e2d\u627e\u4e0d\u5230\u7684\u8bdd\u5c31\u53bb\u5b83\u7684\u7236\u7c7b\u4e2d\u5bfb\u627e.\n", "* `+=` \u8fd0\u7b97\u7b26\u4f1a\u5728\u539f\u5730\u4fee\u6539\u53ef\u53d8\u5bf9\u8c61, \u800c\u4e0d\u662f\u521b\u5efa\u65b0\u5bf9\u8c61. \u56e0\u6b64, \u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b, \u4fee\u6539\u4e00\u4e2a\u5b9e\u4f8b\u7684\u5c5e\u6027\u4f1a\u5f71\u54cd\u5176\u4ed6\u5b9e\u4f8b\u548c\u7c7b\u5c5e\u6027.\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### > yielding None/\u751f\u6210 None\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "some_iterable = ('a', 'b')\n", "\n", "def some_func(val):\n", " return \"something\"\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "**Output:**\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "['a', 'b']\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "[x for x in some_iterable]\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ " at 0x7f70b0a4ad58>\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "[(yield x) for x in some_iterable]\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "['a', 'b']\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "list([(yield x) for x in some_iterable])\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "['a', None, 'b', None]\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "list((yield x) for x in some_iterable)\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "['a', 'something', 'b', 'something']\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "list(some_func((yield x)) for x in some_iterable)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### \ud83d\udca1 \u8bf4\u660e:\n", "- \u6765\u6e90\u548c\u89e3\u91ca\u53ef\u4ee5\u5728\u8fd9\u91cc\u627e\u5230: https://stackoverflow.com/questions/32139885/yield-in-list-comprehensions-and-generator-expressions\n", "- \u76f8\u5173\u9519\u8bef\u62a5\u544a: http://bugs.python.org/issue10544\n", "- \u8fd9\u4e2abug\u57283.7\u4ee5\u540e\u7684\u7248\u672c\u4e2d\u4e0d\u88ab\u63a8\u8350\u4f7f\u7528, \u5e76\u57283.8\u4e2d\u88ab\u4fee\u590d. \u56e0\u6b64\u57283.8\u4e2d\u5c1d\u8bd5\u5728\u63a8\u5bfc\u5f0f\u4e2d\u4f7f\u7528 yield, \u53ea\u4f1a\u5f97\u5230\u4e00\u4e2a SyntaxError. \u8be6\u7ec6\u5185\u5bb9\u53ef\u4ee5\u770b[3.7\u66f4\u65b0\u5185\u5bb9](https://docs.python.org/dev/whatsnew/3.7.html#deprecated-python-behavior), [3.8\u66f4\u65b0\u5185\u5bb9](https://docs.python.org/dev/whatsnew/3.8.html#changes-in-python-behavior).\n", "\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### > Yielding from... return!/\u751f\u6210\u5668\u91cc\u7684return *\n", "1\\.\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "def some_func(x):\n", " if x == 3:\n", " return [\"wtf\"]\n", " else:\n", " yield from range(x)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "**Output (> 3.3):**\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "[]\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "list(some_func(3))\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "`\"wtf\"` \u53bb\u54ea\u513f\u4e86\uff1f\u662f\u56e0\u4e3a`yield from`\u7684\u4e00\u4e9b\u7279\u6b8a\u6548\u679c\u5417\uff1f\u8ba9\u6211\u4eec\u9a8c\u8bc1\u4e00\u4e0b\n", "\n", "2\\.\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "def some_func(x):\n", " if x == 3:\n", " return [\"wtf\"]\n", " else:\n", " for i in range(x):\n", " yield i\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "**Output:**\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "[]\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "list(some_func(3))\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "\u540c\u6837\u7684\u7ed3\u679c\uff0c\u8fd9\u91cc\u4e5f\u4e0d\u8d77\u4f5c\u7528\u3002\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### \ud83d\udca1 \u8bf4\u660e\n", "\n", "+ \u4ece Python 3.3 \u5f00\u59cb\uff0c\u53ef\u4ee5\u5728\u751f\u6210\u5668\u4e2d\u4f7f\u7528\u5e26\u6709\u503c\u7684 `return` \u8bed\u53e5\uff08\u53c2\u89c1 [PEP380](https://www.python.org/dev/peps/pep-0380/)\uff09\u3002 [\u5b98\u65b9\u6587\u6863](https://www.python.org/dev/peps/pep-0380/#enhancements-to-stopiteration) \u63cf\u8ff0\uff0c\n", "\n", "> \"... \u751f\u6210\u5668\u4e2d\u7684 `return expr` \u4f1a\u5bfc\u81f4\u5728\u9000\u51fa\u751f\u6210\u5668\u65f6\u5f15\u53d1 `StopIteration(expr)`\u3002\"\n", "\n", "+ \u5728 `some_func(3)` \u4f8b\u5b50\u4e2d\uff0c`return` \u8bed\u53e5\u5728\u5f00\u59cb\u5c31\u5f15\u53d1\u4e86`StopIteration`\u3002 `StopIteration` \u5f02\u5e38\u4f1a\u5728`list(...)` \u5305\u88c5\u5668\u548c`for` \u5faa\u73af\u4e2d\u81ea\u52a8\u6355\u83b7\u3002 \u56e0\u6b64\uff0c\u4ee5\u4e0a\u4e24\u4e2a\u7247\u6bb5\u90fd\u4ea7\u751f\u7684\u662f\u4e00\u4e2a\u7a7a\u5217\u8868\u3002\n", "\n", "+ \u8981\u4ece\u751f\u6210\u5668 `some_func` \u4e2d\u83b7\u53d6 `[\"wtf\"]`\uff0c\u6211\u4eec\u9700\u8981\u6355\u83b7 `StopIteration` \u5f02\u5e38\uff0c\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ " try:\n", " next(some_func(3))\n", " except StopIteration as e:\n", " some_string = e.value\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ " [\"wtf\"]\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ " some_string\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### > Nan-reflexivity/Nan\u7684\u81ea\u53cd\u6027\n", "1\\.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "a = float('inf')\n", "b = float('nan')\n", "c = float('-iNf') # \u8fd9\u4e9b\u5b57\u7b26\u4e32\u4e0d\u533a\u5206\u5927\u5c0f\u5199\n", "d = float('nan')\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "**Output:**\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "inf\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "a\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "nan\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "b\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "-inf\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "c\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "ValueError: could not convert string to float: some_other_string\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "float('some_other_string')\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "True\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "a == -c #inf==inf\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "True\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "None == None # None==None\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "False\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "b == d #\u4f46\u662f nan!=nan\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "0.0\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "50/a\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "nan\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "a/a\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "nan\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "23 + b\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "2\\.\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "True\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "x = float('nan')\n", "y = x / x\n", "y is y # \u540c\u4e00\u6027(identity)\u5177\u5907\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "False\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "y == y # y\u4e0d\u5177\u5907\u76f8\u7b49\u6027(equality)\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "True\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "[y] == [y] # \u4f46\u5305\u542by\u7684\u5217\u8868\u9a8c\u8bc1\u76f8\u7b49\u6027(equality)\u6210\u529f\u4e86\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### \ud83d\udca1 \u8bf4\u660e:\n", "\n", "`'inf'` \u548c `'nan'` \u662f\u7279\u6b8a\u7684\u5b57\u7b26\u4e32(\u4e0d\u533a\u5206\u5927\u5c0f\u5199), \u5f53\u663e\u793a\u8f6c\u6362\u6210 `float` \u578b\u65f6, \u5b83\u4eec\u5206\u522b\u7528\u4e8e\u8868\u793a\u6570\u5b66\u610f\u4e49\u4e0a\u7684 \"\u65e0\u7a77\u5927\" \u548c \"\u975e\u6570\u5b57\".\n", "- \u7531\u4e8e\u6839\u636e IEEE \u6807\u51c6 `NaN != NaN`\uff0c\u9075\u5b88\u6b64\u89c4\u5219\u6253\u7834\u4e86 Python \u4e2d\u96c6\u5408\u5143\u7d20\u7684\u81ea\u53cd\u6027\u5047\u8bbe\uff0c\u5373\u5982\u679c `x` \u662f `list` \u7b49\u96c6\u5408\u7684\u4e00\u90e8\u5206\uff0c\u5219\u6bd4\u8f83\u7b49\u8fd0\u7b97\u7684\u5b9e\u73b0\u57fa\u4e8e\u5047\u8bbe`x == x`\u3002\u7531\u4e8e\u8fd9\u4e2a\u5047\u8bbe\uff0c\u5728\u6bd4\u8f83\u4e24\u4e2a\u5143\u7d20\u65f6\u9996\u5148\u6bd4\u8f83\u8eab\u4efd`identity`\uff08\u56e0\u4e3a\u5b83\u66f4\u5feb\uff09\uff0c\u5e76\u4e14\u4ec5\u5728\u8eab\u4efd\u4e0d\u5339\u914d\u65f6\u624d\u6bd4\u8f83\u503c\u3002\u4ee5\u4e0b\u7247\u6bb5\u5c06\u66f4\u6e05\u695a\u5730\u8bf4\u660e\uff0c\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ " (False, True)\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ " x = float('nan')\n", " x == x, [x] == [x]\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ " (False, True)\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ " y = float('nan')\n", " y == y, [y] == [y]\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ " (False, False)\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ " x == y, [x] == [y]\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", " \u7531\u4e8e `x` \u548c `y` \u7684\u8eab\u4efd`identity`\u4e0d\u540c\uff0c\u6240\u4ee5\u8003\u8651\u7684\u503c\u4e5f\u4e0d\u540c\uff1b \u56e0\u6b64\u8fd9\u6b21\u6bd4\u8f83\u8fd4\u56de\u201cFalse\u201d\u3002\n", "\n", "\n", "- \u611f\u5174\u8da3\u53ef\u4ee5\u9605\u8bfb [Reflexivity, and other pillars of civilization](https://bertrandmeyer.com/2010/02/06/reflexivity-and-other-pillars-of-civilization/)\n", "\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### > Mutating the immutable!/\u5f3a\u4eba\u6240\u96be\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "some_tuple = (\"A\", \"tuple\", \"with\", \"values\")\n", "another_tuple = ([1, 2], [3, 4], [5, 6])\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "**Output:**\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "TypeError: 'tuple' object does not support item assignment\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "some_tuple[2] = \"change this\"\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "([1, 2], [3, 4], [5, 6, 1000])\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "another_tuple[2].append(1000) # \u8fd9\u91cc\u4e0d\u51fa\u73b0\u9519\u8bef\n", "another_tuple\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "TypeError: 'tuple' object does not support item assignment\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "another_tuple[2] += [99, 999]\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "([1, 2], [3, 4], [5, 6, 1000, 99, 999])\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "another_tuple\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "\u6211\u8fd8\u4ee5\u4e3a\u5143\u7ec4\u662f\u4e0d\u53ef\u53d8\u7684\u5462...\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### \ud83d\udca1 \u8bf4\u660e:\n", "\n", "* \u5f15\u7528 https://docs.python.org/2/reference/datamodel.html\n", "\n", " > \u4e0d\u53ef\u53d8\u5e8f\u5217\n", " \u4e0d\u53ef\u53d8\u5e8f\u5217\u7684\u5bf9\u8c61\u4e00\u65e6\u521b\u5efa\u5c31\u4e0d\u80fd\u518d\u6539\u53d8. (\u5982\u679c\u5bf9\u8c61\u5305\u542b\u5bf9\u5176\u4ed6\u5bf9\u8c61\u7684\u5f15\u7528\uff0c\u5219\u8fd9\u4e9b\u5176\u4ed6\u5bf9\u8c61\u53ef\u80fd\u662f\u53ef\u53d8\u7684\u5e76\u4e14\u53ef\u80fd\u4f1a\u88ab\u4fee\u6539; \u4f46\u662f\uff0c\u7531\u4e0d\u53ef\u53d8\u5bf9\u8c61\u76f4\u63a5\u5f15\u7528\u7684\u5bf9\u8c61\u96c6\u5408\u4e0d\u80fd\u66f4\u6539.)\n", "\n", "* `+=` \u64cd\u4f5c\u7b26\u5728\u539f\u5730\u4fee\u6539\u4e86\u5217\u8868. \u5143\u7d20\u8d4b\u503c\u64cd\u4f5c\u5e76\u4e0d\u5de5\u4f5c, \u4f46\u662f\u5f53\u5f02\u5e38\u629b\u51fa\u65f6, \u5143\u7d20\u5df2\u7ecf\u5728\u539f\u5730\u88ab\u4fee\u6539\u4e86.\n", "\n", "(\u8bd1: \u5bf9\u4e8e\u4e0d\u53ef\u53d8\u5bf9\u8c61, \u8fd9\u91cc\u6307tuple, `+=` \u5e76\u4e0d\u662f\u539f\u5b50\u64cd\u4f5c, \u800c\u662f `extend` \u548c `=` \u4e24\u4e2a\u52a8\u4f5c, \u8fd9\u91cc `=` \u64cd\u4f5c\u867d\u7136\u4f1a\u629b\u51fa\u5f02\u5e38, \u4f46 `extend` \u64cd\u4f5c\u5df2\u7ecf\u4fee\u6539\u6210\u529f\u4e86. \u8be6\u7ec6\u89e3\u91ca\u53ef\u4ee5\u770b[\u8fd9\u91cc](https://segmentfault.com/a/1190000010767068))\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### > The disappearing variable from outer scope/\u6d88\u5931\u7684\u5916\u90e8\u53d8\u91cf\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "e = 7\n", "try:\n", " raise Exception()\n", "except Exception as e:\n", " pass\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "**Output (Python 2.x):**\n" ] }, { "cell_type": "code", "metadata": { "collapsed": true }, "execution_count": null, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "# prints nothing\n" ] } ], "source": [ "print(e)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "**Output (Python 3.x):**\n" ] }, { "cell_type": "code", "metadata": { "collapsed": true }, "execution_count": null, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "NameError: name 'e' is not defined\n" ] } ], "source": [ "print(e)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### \ud83d\udca1 \u8bf4\u660e:\n", "\n", "* \u51fa\u5904: https://docs.python.org/3/reference/compound_stmts.html#except\n", "\n", " \u5f53\u4f7f\u7528 `as` \u4e3a\u76ee\u6807\u5206\u914d\u5f02\u5e38\u7684\u65f6\u5019, \u5c06\u5728except\u5b50\u53e5\u7684\u672b\u5c3e\u6e05\u9664\u8be5\u5f02\u5e38.\n", "\n", " \u8fd9\u5c31\u597d\u50cf\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ " except E as N:\n", " foo\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", " \u4f1a\u88ab\u7ffb\u8bd1\u6210\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ " except E as N:\n", " try:\n", " foo\n", " finally:\n", " del N\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", " \u8fd9\u610f\u5473\u7740\u5f02\u5e38\u5fc5\u987b\u5728\u88ab\u8d4b\u503c\u7ed9\u5176\u4ed6\u53d8\u91cf\u624d\u80fd\u5728 `except` \u5b50\u53e5\u4e4b\u540e\u5f15\u7528\u5b83. \u800c\u5f02\u5e38\u4e4b\u6240\u4ee5\u4f1a\u88ab\u6e05\u9664, \u5219\u662f\u7531\u4e8e\u4e0a\u9762\u9644\u52a0\u7684\u56de\u6eaf\u4fe1\u606f(trackback)\u4f1a\u548c\u6808\u5e27(stack frame)\u5f62\u6210\u5faa\u73af\u5f15\u7528, \u4f7f\u5f97\u8be5\u6808\u5e27\u4e2d\u7684\u6240\u6709\u672c\u5730\u53d8\u91cf\u5728\u4e0b\u4e00\u6b21\u5783\u573e\u56de\u6536\u53d1\u751f\u4e4b\u524d\u90fd\u5904\u4e8e\u6d3b\u52a8\u72b6\u6001.(\u8bd1: \u4e5f\u5c31\u662f\u8bf4\u4e0d\u4f1a\u88ab\u56de\u6536)\n", "\n", "* \u5b50\u53e5\u5728 Python \u4e2d\u5e76\u6ca1\u6709\u72ec\u7acb\u7684\u4f5c\u7528\u57df. \u793a\u4f8b\u4e2d\u7684\u6240\u6709\u5185\u5bb9\u90fd\u5904\u4e8e\u540c\u4e00\u4f5c\u7528\u57df\u5185, \u6240\u4ee5\u53d8\u91cf `e` \u4f1a\u7531\u4e8e\u6267\u884c\u4e86 `except` \u5b50\u53e5\u800c\u88ab\u5220\u9664. \u800c\u5bf9\u4e8e\u6709\u72ec\u7acb\u7684\u5185\u90e8\u4f5c\u7528\u57df\u7684\u51fd\u6570\u6765\u8bf4\u60c5\u51b5\u5c31\u4e0d\u4e00\u6837\u4e86. \u4e0b\u9762\u7684\u4f8b\u5b50\u8bf4\u660e\u4e86\u8fd9\u4e00\u70b9:\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ " def f(x):\n", " del(x)\n", " print(x)\n", "\n", " x = 5\n", " y = [5, 4, 3]\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", " **Output:**\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ " 5\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ " >>>f(x)\n", " UnboundLocalError: local variable 'x' referenced before assignment\n", " >>>f(y)\n", " UnboundLocalError: local variable 'x' referenced before assignment\n", " x\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ " [5, 4, 3]\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ " y\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "* \u5728 Python 2.x \u4e2d, `Exception()` \u5b9e\u4f8b\u88ab\u8d4b\u503c\u7ed9\u4e86\u53d8\u91cf `e`, \u6240\u4ee5\u5f53\u4f60\u5c1d\u8bd5\u6253\u5370\u7ed3\u679c\u7684\u65f6\u5019, \u5b83\u7684\u8f93\u51fa\u4e3a\u7a7a.\uff08\u8bd1: \u6b63\u5e38\u7684Exception\u5b9e\u4f8b\u6253\u5370\u51fa\u6765\u5c31\u662f\u7a7a\uff09\n", "\n", " **Output (Python 2.x):**\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ " Exception()\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ " e\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ " # \u6ca1\u6709\u6253\u5370\u4efb\u4f55\u5185\u5bb9!\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ " print e\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### > The mysterious key type conversion/\u795e\u79d8\u7684\u952e\u578b\u8f6c\u6362 *\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "class SomeClass(str):\n", " pass\n", "\n", "some_dict = {'s':42}\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "**Output:**\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "str\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "type(list(some_dict.keys())[0])\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "{'s': 40}\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "s = SomeClass('s')\n", "some_dict[s] = 40\n", "some_dict # \u9884\u671f: \u4e24\u4e2a\u4e0d\u540c\u7684\u952e\u503c\u5bf9\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "str\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "type(list(some_dict.keys())[0])\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### \ud83d\udca1 \u8bf4\u660e:\n", "\n", "* \u7531\u4e8e `SomeClass` \u4f1a\u4ece `str` \u81ea\u52a8\u7ee7\u627f `__hash__` \u65b9\u6cd5, \u6240\u4ee5 `s` \u5bf9\u8c61\u548c `\"s\"` \u5b57\u7b26\u4e32\u7684\u54c8\u5e0c\u503c\u662f\u76f8\u540c\u7684.\n", "* \u800c `SomeClass(\"s\") == \"s\"` \u4e3a `True` \u662f\u56e0\u4e3a `SomeClass` \u4e5f\u7ee7\u627f\u4e86 `str` \u7c7b `__eq__` \u65b9\u6cd5.\n", "* \u7531\u4e8e\u4e24\u8005\u7684\u54c8\u5e0c\u503c\u76f8\u540c\u4e14\u76f8\u7b49, \u6240\u4ee5\u5b83\u4eec\u5728\u5b57\u5178\u4e2d\u8868\u793a\u76f8\u540c\u7684\u952e.\n", "* \u5982\u679c\u60f3\u8981\u5b9e\u73b0\u671f\u671b\u7684\u529f\u80fd, \u6211\u4eec\u53ef\u4ee5\u91cd\u5b9a\u4e49 `SomeClass` \u7684 `__eq__` \u65b9\u6cd5.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ " class SomeClass(str):\n", " def __eq__(self, other):\n", " return (\n", " type(self) is SomeClass\n", " and type(other) is SomeClass\n", " and super().__eq__(other)\n", " )\n", "\n", " # \u5f53\u6211\u4eec\u81ea\u5b9a\u4e49 __eq__ \u65b9\u6cd5\u65f6, Python \u4e0d\u4f1a\u518d\u81ea\u52a8\u7ee7\u627f __hash__ \u65b9\u6cd5\n", " # \u6240\u4ee5\u6211\u4eec\u4e5f\u9700\u8981\u5b9a\u4e49\u5b83\n", " __hash__ = str.__hash__\n", "\n", " some_dict = {'s':42}\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", " **Output:**\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ " {'s': 40, 's': 42}\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ " s = SomeClass('s')\n", " some_dict[s] = 40\n", " some_dict\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ " (__main__.SomeClass, str)\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ " keys = list(some_dict.keys())\n", " type(keys[0]), type(keys[1])\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### > Let's see if you can guess this?/\u770b\u770b\u4f60\u80fd\u5426\u731c\u5230\u8fd9\u4e00\u70b9?\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "a, b = a[b] = {}, 5\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "**Output:**\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "{5: ({...}, 5)}\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "a\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### \ud83d\udca1 \u8bf4\u660e:\n", "\n", "* \u6839\u636e [Python \u8bed\u8a00\u53c2\u8003](https://docs.python.org/2/reference/simple_stmts.html#assignment-statements), \u8d4b\u503c\u8bed\u53e5\u7684\u5f62\u5f0f\u5982\u4e0b\n", " ```\n", " (target_list \"=\")+ (expression_list | yield_expression)\n", " ```\n", "\n", " > \u8d4b\u503c\u8bed\u53e5\u8ba1\u7b97\u8868\u8fbe\u5f0f\u5217\u8868(expression list)(\u7262\u8bb0 \u8fd9\u53ef\u4ee5\u662f\u5355\u4e2a\u8868\u8fbe\u5f0f\u6216\u4ee5\u9017\u53f7\u5206\u9694\u7684\u5217\u8868, \u540e\u8005\u8fd4\u56de\u5143\u7ec4)\u5e76\u5c06\u5355\u4e2a\u7ed3\u679c\u5bf9\u8c61\u4ece\u5de6\u5230\u53f3\u5206\u914d\u7ed9\u76ee\u6807\u5217\u8868\u4e2d\u7684\u6bcf\u4e00\u9879.\n", "\n", "* `(target_list \"=\")+` \u4e2d\u7684 `+` \u610f\u5473\u7740\u53ef\u4ee5\u6709**\u4e00\u4e2a\u6216\u591a\u4e2a**\u76ee\u6807\u5217\u8868. \u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d, \u76ee\u6807\u5217\u8868\u662f `a, b` \u548c `a[b]` (\u6ce8\u610f\u8868\u8fbe\u5f0f\u5217\u8868\u53ea\u80fd\u6709\u4e00\u4e2a, \u5728\u6211\u4eec\u7684\u4f8b\u5b50\u4e2d\u662f `{}, 5`).\n", "\n", "* \u8868\u8fbe\u5f0f\u5217\u8868\u8ba1\u7b97\u7ed3\u675f\u540e, \u5c06\u5176\u503c\u81ea\u52a8\u89e3\u5305\u540e**\u4ece\u5de6\u5230\u53f3**\u5206\u914d\u7ed9\u76ee\u6807\u5217\u8868(target list). \u56e0\u6b64, \u5728\u6211\u4eec\u7684\u4f8b\u5b50\u4e2d, \u9996\u5148\u5c06 `{}, 5` \u5143\u7ec4\u5e76\u8d4b\u503c\u7ed9 `a, b`, \u7136\u540e\u6211\u4eec\u5c31\u53ef\u4ee5\u5f97\u5230 `a = {}` \u4e14 `b = 5`.\n", "\n", "* `a` \u88ab\u8d4b\u503c\u7684 `{}` \u662f\u53ef\u53d8\u5bf9\u8c61.\n", "\n", "* \u7b2c\u4e8c\u4e2a\u76ee\u6807\u5217\u8868\u662f `a[b]` (\u4f60\u53ef\u80fd\u89c9\u5f97\u8fd9\u91cc\u4f1a\u62a5\u9519, \u56e0\u4e3a\u5728\u4e4b\u524d\u7684\u8bed\u53e5\u4e2d `a` \u548c `b` \u90fd\u8fd8\u6ca1\u6709\u88ab\u5b9a\u4e49. \u4f46\u662f\u522b\u5fd8\u4e86, \u6211\u4eec\u521a\u521a\u5c06 `a` \u8d4b\u503c `{}` \u4e14\u5c06 `b` \u8d4b\u503c\u4e3a `5`).\n", "\n", "* \u73b0\u5728, \u6211\u4eec\u5c06\u901a\u8fc7\u5c06\u5b57\u5178\u4e2d\u952e `5` \u7684\u503c\u8bbe\u7f6e\u4e3a\u5143\u7ec4 `({}, 5)` \u6765\u521b\u5efa\u5faa\u73af\u5f15\u7528 (\u8f93\u51fa\u4e2d\u7684 `{...}` \u6307\u4e0e `a` \u5f15\u7528\u4e86\u76f8\u540c\u7684\u5bf9\u8c61). \u4e0b\u9762\u662f\u4e00\u4e2a\u66f4\u7b80\u5355\u7684\u5faa\u73af\u5f15\u7528\u7684\u4f8b\u5b50\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ " [[...]]\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ " some_list = some_list[0] = [0]\n", " some_list\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ " [[...]]\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ " some_list[0]\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ " True\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ " some_list is some_list[0]\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ " True\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ " some_list[0][0][0][0][0][0] == some_list\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " \u6211\u4eec\u7684\u4f8b\u5b50\u5c31\u662f\u8fd9\u79cd\u60c5\u51b5 (`a[b][0]` \u4e0e `a` \u662f\u76f8\u540c\u7684\u5bf9\u8c61)\n", "\n", "* \u603b\u7ed3\u4e00\u4e0b, \u4f60\u4e5f\u53ef\u4ee5\u628a\u4f8b\u5b50\u62c6\u6210\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ " a, b = {}, 5\n", " a[b] = a, b\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " \u5e76\u4e14\u53ef\u4ee5\u901a\u8fc7 `a[b][0]` \u4e0e `a` \u662f\u76f8\u540c\u7684\u5bf9\u8c61\u6765\u8bc1\u660e\u662f\u5faa\u73af\u5f15\u7528\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ " True\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ " a[b][0] is a\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### > Exceeds the limit for integer string conversion/\u6574\u578b\u8f6c\u5b57\u7b26\u4e32\u8d8a\u754c\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "# Python 3.10.6\n", "int(\"2\" * 5432)\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "# Python 3.10.8\n", "int(\"2\" * 5432)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "**Output:**\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "222222222222222222222222222222222222222222222222222222222222222...\n", "\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "# Python 3.10.6\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "Traceback (most recent call last):\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "# Python 3.10.8\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "ValueError: Exceeds the limit (4300) for integer string conversion:\n", " value has 5432 digits; use sys.set_int_max_str_digits()\n", " to increase the limit.\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ " \n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### \ud83d\udca1 \u8bf4\u660e:\n", "\n", "* \u5bf9`int()`\u7684\u8c03\u7528\u5728Python 3.10.6\u4e2d\u8fd0\u884c\u826f\u597d\uff0c\u4f46\u5728Python 3.10.8\u4e2d\u5f15\u53d1ValueError\u3002\u8bf7\u6ce8\u610f\uff0cPython\u4ecd\u7136\u53ef\u4ee5\u5904\u7406\u5927\u6574\u6570\u3002\u53ea\u6709\u5728\u6574\u578b\u548c\u5b57\u7b26\u4e32\u4e4b\u95f4\u8f6c\u6362\u65f6\u624d\u4f1a\u51fa\u73b0\u6b64\u9519\u8bef\u3002\n", "* \u5e78\u8fd0\u7684\u662f\uff0c\u5f53\u60a8\u5e0c\u671b\u64cd\u4f5c\u8d85\u8fc7\u5141\u8bb8\u7684\u4f4d\u6570\u9650\u5236\u65f6\uff0c\u53ef\u4ee5\u589e\u52a0\u8be5\u9650\u5236\u7684\u4e0a\u9650\u3002\u4e3a\u6b64\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u65b9\u6cd5\u4e4b\u4e00\uff1a\n", " - \u4f7f\u7528 -X int_max_str_digits \u7684\u547d\u4ee4\u884c\u53c2\u6570\uff08\u4f8b\u5982\uff0c python3 -X int_max_str_digits=640\uff09\n", " - \u4f7f\u7528\u6765\u81easys\u6a21\u5757\u7684set_int_max_str_digits()\u51fd\u6570\n", " - \u8bbe\u5b9a PYTHONINTMAXSTRDIGITS \u73af\u5883\u53d8\u91cf\n", "\n", "\u66f4\u591a\u66f4\u6539\u8bbe\u7f6e\u4e0a\u9650\u7684\u64cd\u4f5c\u7ec6\u8282\u67e5\u770b[\u6587\u6863](https://docs.python.org/3/library/stdtypes.html#int-max-str-digits)\u3002\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### > Modifying a dictionary while iterating over it/\u8fed\u4ee3\u5b57\u5178\u65f6\u7684\u4fee\u6539\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "x = {0: None}\n", "\n", "for i in x:\n", " del x[i]\n", " x[i+1] = None\n", " print(i)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "**Output (Python 2.7- Python 3.5):**\n", "\n", "```\n", "0\n", "1\n", "2\n", "3\n", "4\n", "5\n", "6\n", "7\n", "```\n", "\n", "\u662f\u7684, \u5b83\u8fd0\u884c\u4e86**\u516b\u6b21**\u7136\u540e\u624d\u505c\u4e0b\u6765.\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### \ud83d\udca1 \u8bf4\u660e:\n", "\n", "* Python\u4e0d\u652f\u6301\u5bf9\u5b57\u5178\u8fdb\u884c\u8fed\u4ee3\u7684\u540c\u65f6\u4fee\u6539\u5b83.\n", "* \u5b83\u4e4b\u6240\u4ee5\u8fd0\u884c8\u6b21, \u662f\u56e0\u4e3a\u5b57\u5178\u4f1a\u81ea\u52a8\u6269\u5bb9\u4ee5\u5bb9\u7eb3\u66f4\u591a\u952e\u503c(\u6211\u4eec\u67098\u6b21\u5220\u9664\u8bb0\u5f55, \u56e0\u6b64\u9700\u8981\u6269\u5bb9). \u8fd9\u5b9e\u9645\u4e0a\u662f\u4e00\u4e2a\u5b9e\u73b0\u7ec6\u8282. (\u8bd1: \u5e94\u8be5\u662f\u56e0\u4e3a\u5b57\u5178\u7684\u521d\u59cb\u6700\u5c0f\u503c\u662f8, \u6269\u5bb9\u4f1a\u5bfc\u81f4\u6563\u5217\u8868\u5730\u5740\u53d1\u751f\u53d8\u5316\u800c\u4e2d\u65ad\u5faa\u73af.)\n", "* \u5728\u4e0d\u540c\u7684Python\u5b9e\u73b0\u4e2d\u5220\u9664\u952e\u7684\u5904\u7406\u65b9\u5f0f\u4ee5\u53ca\u8c03\u6574\u5927\u5c0f\u7684\u65f6\u95f4\u53ef\u80fd\u4f1a\u6709\u6240\u4e0d\u540c.(\u8bd1: \u5c31\u662f\u8bf4\u4ec0\u4e48\u65f6\u5019\u6269\u5bb9\u5728\u4e0d\u540c\u7248\u672c\u4e2d\u53ef\u80fd\u662f\u4e0d\u540c\u7684, \u57283.6\u53ca3.7\u7684\u7248\u672c\u4e2d\u5230[5](https://github.com/python/cpython/blob/v3.6.1/Objects/dictobject.c#L103-L110)\u5c31\u4f1a\u81ea\u52a8\u6269\u5bb9\u4e86. \u4ee5\u540e\u4e5f\u6709\u53ef\u80fd\u518d\u6b21\u53d1\u751f\u53d8\u5316. \u8fd9\u662f\u4e3a\u4e86\u907f\u514d\u6563\u5217\u51b2\u7a81. \u987a\u5e26\u4e00\u63d0, \u540e\u9762\u4e24\u6b21\u6269\u5bb9\u4f1a\u6269\u5c55\u4e3a32\u548c256. \u5373`8->32->256`.)\n", "* \u66f4\u591a\u7684\u4fe1\u606f, \u4f60\u53ef\u4ee5\u53c2\u8003\u8fd9\u4e2aStackOverflow\u7684[\u56de\u7b54](https://stackoverflow.com/questions/44763802/bug-in-python-dict), \u5b83\u8be6\u7ec6\u7684\u89e3\u91ca\u4e00\u4e2a\u7c7b\u4f3c\u7684\u4f8b\u5b50.\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### > Stubborn `del` operator/\u575a\u5f3a\u7684 `del` *\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "class SomeClass:\n", " def __del__(self):\n", " print(\"Deleted!\")\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "**Output:**\n", "1\\.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "Deleted!\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "x = SomeClass()\n", "y = x\n", "del x # \u8fd9\u91cc\u5e94\u8be5\u4f1a\u8f93\u51fa \"Deleted!\"\n", "del y\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "\u5537, \u7ec8\u4e8e\u5220\u9664\u4e86. \u4f60\u53ef\u80fd\u5df2\u7ecf\u731c\u5230\u4e86\u5728\u6211\u4eec\u7b2c\u4e00\u6b21\u5c1d\u8bd5\u5220\u9664 `x` \u65f6\u662f\u4ec0\u4e48\u8ba9 `__del__` \u514d\u4e8e\u88ab\u8c03\u7528\u7684. \u90a3\u8ba9\u6211\u4eec\u7ed9\u8fd9\u4e2a\u4f8b\u5b50\u589e\u52a0\u70b9\u96be\u5ea6.\n", "\n", "2\\.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "<__main__.SomeClass instance at 0x7f98a1a67fc8>\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "x = SomeClass()\n", "y = x\n", "del x\n", "y # \u68c0\u67e5\u4e00\u4e0by\u662f\u5426\u5b58\u5728\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "Deleted!\n", "{'__builtins__': , 'SomeClass': , '__package__': None, '__name__': '__main__', '__doc__': None}\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "del y # \u50cf\u4e4b\u524d\u4e00\u6837, \u8fd9\u91cc\u5e94\u8be5\u4f1a\u8f93\u51fa \"Deleted!\"\n", "globals() # \u597d\u5427, \u5e76\u6ca1\u6709. \u8ba9\u6211\u4eec\u770b\u4e00\u4e0b\u6240\u6709\u7684\u5168\u5c40\u53d8\u91cf\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "\u597d\u4e86\uff0c\u73b0\u5728\u5b83\u88ab\u5220\u9664\u4e86 :confused:\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### \ud83d\udca1 \u8bf4\u660e:\n", "+ `del x` \u5e76\u4e0d\u4f1a\u7acb\u523b\u8c03\u7528 `x.__del__()`.\n", "+ \u6bcf\u5f53\u9047\u5230 `del x`, Python \u4f1a\u5c06 `x` \u7684\u5f15\u7528\u6570\u51cf1, \u5f53 `x` \u7684\u5f15\u7528\u6570\u51cf\u52300\u65f6\u5c31\u4f1a\u8c03\u7528 `x.__del__()`.\n", "+ \u5728\u7b2c\u4e8c\u4e2a\u4f8b\u5b50\u4e2d, `y.__del__()` \u4e4b\u6240\u4ee5\u672a\u88ab\u8c03\u7528, \u662f\u56e0\u4e3a\u524d\u4e00\u6761\u8bed\u53e5 (`>>> y`) \u5bf9\u540c\u4e00\u5bf9\u8c61\u521b\u5efa\u4e86\u53e6\u4e00\u4e2a\u5f15\u7528, \u4ece\u800c\u9632\u6b62\u5728\u6267\u884c `del y` \u540e\u5bf9\u8c61\u7684\u5f15\u7528\u6570\u53d8\u4e3a0.\n", "+ \u8c03\u7528 `globals` \u5bfc\u81f4\u5f15\u7528\u88ab\u9500\u6bc1, \u56e0\u6b64\u6211\u4eec\u53ef\u4ee5\u770b\u5230 \"Deleted!\" \u7ec8\u4e8e\u88ab\u8f93\u51fa\u4e86.\n", "+ (\u8bd1: \u8fd9\u5176\u5b9e\u662f Python \u4ea4\u4e92\u89e3\u91ca\u5668\u7684\u7279\u6027, \u5b83\u4f1a\u81ea\u52a8\u8ba9 `_` \u4fdd\u5b58\u4e0a\u4e00\u4e2a\u8868\u8fbe\u5f0f\u8f93\u51fa\u7684\u503c, \u8be6\u7ec6\u53ef\u4ee5\u770b[\u8fd9\u91cc](https://www.cnblogs.com/leisurelylicht/p/diao-pi-de-kong-zhi-tai.html).)\n", "\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### > The out of scope variable/\u5916\u90e8\u4f5c\u7528\u57df\u53d8\u91cf\n", "1\\.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "a = 1\n", "def some_func():\n", " return a\n", "\n", "def another_func():\n", " a += 1\n", " return a\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "2\\.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "def some_closure_func():\n", " a = 1\n", " def some_inner_func():\n", " return a\n", " return some_inner_func()\n", "\n", "def another_closure_func():\n", " a = 1\n", " def another_inner_func():\n", " a += 1\n", " return a\n", " return another_inner_func()\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "**Output:**\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "1\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "some_func()\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "UnboundLocalError: local variable 'a' referenced before assignment\n", "\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "another_func()\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "1\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "some_closure_func()\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "UnboundLocalError: local variable 'a' referenced before assignment\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "another_closure_func()\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### \ud83d\udca1 \u8bf4\u660e:\n", "* \u5f53\u4f60\u5728\u4f5c\u7528\u57df\u4e2d\u5bf9\u53d8\u91cf\u8fdb\u884c\u8d4b\u503c\u65f6, \u53d8\u91cf\u4f1a\u53d8\u6210\u8be5\u4f5c\u7528\u57df\u5185\u7684\u5c40\u90e8\u53d8\u91cf. \u56e0\u6b64 `a` \u4f1a\u53d8\u6210 `another_func` \u51fd\u6570\u4f5c\u7528\u57df\u4e2d\u7684\u5c40\u90e8\u53d8\u91cf, \u4f46\u5b83\u5728\u51fd\u6570\u4f5c\u7528\u57df\u4e2d\u5e76\u6ca1\u6709\u88ab\u521d\u59cb\u5316, \u6240\u4ee5\u4f1a\u5f15\u53d1\u9519\u8bef.\n", "* \u60f3\u8981\u5728 `another_func` \u4e2d\u4fee\u6539\u5916\u90e8\u4f5c\u7528\u57df\u53d8\u91cf `a` \u7684\u8bdd, \u53ef\u4ee5\u4f7f\u7528 `global` \u5173\u952e\u5b57.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ " def another_func()\n", " global a\n", " a += 1\n", " return a\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", " **Output:**\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ " 2\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ " another_func()\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "* \u5728 `another_closure_func` \u51fd\u6570\u4e2d\uff0c`a` \u4f1a\u53d8\u6210 `another_inner_func` \u51fd\u6570\u4f5c\u7528\u57df\u4e2d\u7684\u5c40\u90e8\u53d8\u91cf, \u4f46\u5b83\u5728\u540c\u4e00\u4f5c\u7528\u57df\u4e2d\u5e76\u6ca1\u6709\u88ab\u521d\u59cb\u5316, \u6240\u4ee5\u4f1a\u5f15\u53d1\u9519\u8bef\u3002\n", "* \u60f3\u8981\u5728 `another_inner_func` \u4e2d\u4fee\u6539\u5916\u90e8\u4f5c\u7528\u57df\u53d8\u91cf `a` \u7684\u8bdd, \u53ef\u4ee5\u4f7f\u7528 `nonlocal` \u5173\u952e\u5b57\u3002nonlocal \u8868\u8fbe\u5f0f\u7528\u4e8e\uff08\u9664\u5168\u5c40\u4f5c\u7528\u57df\u5916\uff09\u6700\u8fd1\u4e00\u7ea7\u7684\u5916\u90e8\u4f5c\u7528\u57df\u3002\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ " def another_func():\n", " a = 1\n", " def another_inner_func():\n", " nonlocal a\n", " a += 1\n", " return a\n", " return another_inner_func()\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", " **Output:**\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ " 2\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ " another_func()\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "* `global` and `nonlocal` \u5173\u952e\u5b57\u544a\u8bc9 `Python` \u89e3\u91ca\u5668\uff0c\u4e0d\u8981\u58f0\u660e\u65b0\u53d8\u91cf\uff0c\u800c\u662f\u5728\u76f8\u5e94\u7684\u5916\u90e8\u4f5c\u7528\u57df\u4e2d\u67e5\u627e\u53d8\u91cf\u3002\n", "* \u53ef\u4ee5\u9605\u8bfb[\u8fd9\u4e2a](https://sebastianraschka.com/Articles/2014_python_scope_and_namespaces.html)\u7b80\u77ed\u5374\u5f88\u68d2\u7684\u6307\u5357, \u4e86\u89e3\u66f4\u591a\u5173\u4e8e Python \u4e2d\u547d\u540d\u7a7a\u95f4\u548c\u4f5c\u7528\u57df\u7684\u5de5\u4f5c\u539f\u7406\u3002\n", "\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### > Deleting a list item while iterating/\u8fed\u4ee3\u5217\u8868\u65f6\u5220\u9664\u5143\u7d20\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "list_1 = [1, 2, 3, 4]\n", "list_2 = [1, 2, 3, 4]\n", "list_3 = [1, 2, 3, 4]\n", "list_4 = [1, 2, 3, 4]\n", "\n", "for idx, item in enumerate(list_1):\n", " del item\n", "\n", "for idx, item in enumerate(list_2):\n", " list_2.remove(item)\n", "\n", "for idx, item in enumerate(list_3[:]):\n", " list_3.remove(item)\n", "\n", "for idx, item in enumerate(list_4):\n", " list_4.pop(idx)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "**Output:**\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 4]\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "list_1\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "[2, 4]\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "list_2\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "[]\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "list_3\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "[2, 4]\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "list_4\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "\u4f60\u80fd\u731c\u5230\u4e3a\u4ec0\u4e48\u8f93\u51fa\u662f `[2, 4]` \u5417?\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### \ud83d\udca1 \u8bf4\u660e:\n", "\n", "* \u5728\u8fed\u4ee3\u65f6\u4fee\u6539\u5bf9\u8c61\u662f\u4e00\u4e2a\u5f88\u611a\u8822\u7684\u4e3b\u610f. \u6b63\u786e\u7684\u505a\u6cd5\u662f\u8fed\u4ee3\u5bf9\u8c61\u7684\u526f\u672c, `list_3[:]` \u5c31\u662f\u8fd9\u4e48\u505a\u7684.\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ " 139798789457608\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ " some_list = [1, 2, 3, 4]\n", " id(some_list)\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ " 139798779601192\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ " id(some_list[:]) # \u6ce8\u610fpython\u4e3a\u5207\u7247\u5217\u8868\u521b\u5efa\u4e86\u65b0\u5bf9\u8c61.\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "**`del`, `remove` \u548c `pop` \u7684\u4e0d\u540c:**\n", "* `del var_name` \u53ea\u662f\u4ece\u672c\u5730\u6216\u5168\u5c40\u547d\u540d\u7a7a\u95f4\u4e2d\u5220\u9664\u4e86 `var_name` (\u8fd9\u5c31\u662f\u4e3a\u4ec0\u4e48 `list_1` \u6ca1\u6709\u53d7\u5230\u5f71\u54cd).\n", "* `remove` \u4f1a\u5220\u9664\u7b2c\u4e00\u4e2a\u5339\u914d\u5230\u7684\u6307\u5b9a\u503c, \u800c\u4e0d\u662f\u7279\u5b9a\u7684\u7d22\u5f15, \u5982\u679c\u627e\u4e0d\u5230\u503c\u5219\u629b\u51fa `ValueError` \u5f02\u5e38.\n", "* `pop` \u5219\u4f1a\u5220\u9664\u6307\u5b9a\u7d22\u5f15\u5904\u7684\u5143\u7d20\u5e76\u8fd4\u56de\u5b83, \u5982\u679c\u6307\u5b9a\u4e86\u65e0\u6548\u7684\u7d22\u5f15\u5219\u629b\u51fa `IndexError` \u5f02\u5e38.\n", "\n", "**\u4e3a\u4ec0\u4e48\u8f93\u51fa\u662f `[2, 4]`?**\n", "- \u5217\u8868\u8fed\u4ee3\u662f\u6309\u7d22\u5f15\u8fdb\u884c\u7684, \u6240\u4ee5\u5f53\u6211\u4eec\u4ece `list_2` \u6216 `list_4` \u4e2d\u5220\u9664 `1` \u65f6, \u5217\u8868\u7684\u5185\u5bb9\u5c31\u53d8\u6210\u4e86 `[2, 3, 4]`. \u5269\u4f59\u5143\u7d20\u4f1a\u4f9d\u6b21\u4f4d\u79fb, \u4e5f\u5c31\u662f\u8bf4, `2` \u7684\u7d22\u5f15\u4f1a\u53d8\u4e3a 0, `3` \u4f1a\u53d8\u4e3a 1. \u7531\u4e8e\u4e0b\u4e00\u6b21\u8fed\u4ee3\u5c06\u83b7\u53d6\u7d22\u5f15\u4e3a 1 \u7684\u5143\u7d20 (\u5373 `3`), \u56e0\u6b64 `2` \u5c06\u88ab\u5f7b\u5e95\u7684\u8df3\u8fc7. \u7c7b\u4f3c\u7684\u60c5\u51b5\u4f1a\u4ea4\u66ff\u53d1\u751f\u5728\u5217\u8868\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u4e0a.\n", "\n", "* \u53c2\u8003\u8fd9\u4e2aStackOverflow\u7684[\u56de\u7b54](https://stackoverflow.com/questions/45946228/what-happens-when-you-try-to-delete-a-list-element-while-iterating-over-it)\u6765\u89e3\u91ca\u8fd9\u4e2a\u4f8b\u5b50\n", "* \u5173\u4e8ePython\u4e2d\u5b57\u5178\u7684\u7c7b\u4f3c\u4f8b\u5b50, \u53ef\u4ee5\u53c2\u8003\u8fd9\u4e2aStackoverflow\u7684[\u56de\u7b54](https://stackoverflow.com/questions/45877614/how-to-change-all-the-dictionary-keys-in-a-for-loop-with-d-items).\n", "\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### > Lossy zip of iterators/\u4e22\u4e09\u843d\u56db\u7684zip *\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "[0, 1, 2, 3, 4, 5, 6]\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "numbers = list(range(7))\n", "numbers\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "([0, 1, 2], [3, 4, 5, 6])\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "first_three, remaining = numbers[:3], numbers[3:]\n", "first_three, remaining\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "[(0, 0), (1, 1), (2, 2)]\n", "# so far so good, let's zip the remaining\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "numbers_iter = iter(numbers)\n", "list(zip(numbers_iter, first_three)) \n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "[(4, 3), (5, 4), (6, 5)]\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "list(zip(numbers_iter, remaining))\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "`numbers` \u5217\u8868\u4e2d\u7684\u5143\u7d20 `3` \u54ea\u91cc\u53bb\u4e86\uff1f\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### \ud83d\udca1 \u8bf4\u660e\n", "\n", "- \u6839\u636ePython [\u6587\u6863](https://docs.python.org/3.3/library/functions.html#zip)\uff0c `zip` \u51fd\u6570\u7684\u5927\u6982\u5b9e\u73b0\u5982\u4e0b\uff1a\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ " def zip(*iterables):\n", " sentinel = object()\n", " iterators = [iter(it) for it in iterables]\n", " while iterators:\n", " result = []\n", " for it in iterators:\n", " elem = next(it, sentinel)\n", " if elem is sentinel: return\n", " result.append(elem)\n", " yield tuple(result)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "- \u8be5\u51fd\u6570\u63a5\u53d7\u4efb\u610f\u6570\u91cf\u7684\u53ef\u8fed\u4ee3\u5bf9\u8c61\uff0c\u901a\u8fc7\u8c03\u7528 `next` \u51fd\u6570\u5c06\u5b83\u4eec\u7684\u6bcf\u4e2a\u9879\u76ee\u6dfb\u52a0\u5230 `result` \u5217\u8868\u4e2d\uff0c\u5e76\u5728\u4efb\u4e00\u53ef\u8fed\u4ee3\u5bf9\u8c61\u8017\u5c3d\u65f6\u505c\u6b62\u3002\n", "- \u8fd9\u91cc\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u5f53\u4efb\u4e00\u53ef\u8fed\u4ee3\u5bf9\u8c61\u7528\u5c3d\u65f6\uff0c`result` \u5217\u8868\u4e2d\u7684\u73b0\u6709\u5143\u7d20\u5c06\u88ab\u4e22\u5f03\u3002\u8fd9\u5c31\u662f `numbers_iter` \u4e2d\u7684 `3` \u6240\u53d1\u751f\u7684\u60c5\u51b5\u3002\n", "- \u4f7f\u7528 zip \u6267\u884c\u4e0a\u8ff0\u64cd\u4f5c\u7684\u6b63\u786e\u65b9\u6cd5\u662f:\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ " [(0, 0), (1, 1), (2, 2)]\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ " numbers = list(range(7))\n", " numbers_iter = iter(numbers)\n", " list(zip(first_three, numbers_iter))\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ " [(3, 3), (4, 4), (5, 5), (6, 6)]\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ " list(zip(remaining, numbers_iter))\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", " `zip` \u7684\u7b2c\u4e00\u4e2a\u53c2\u6570\u5e94\u5f53\u662f\u6709\u6700\u5c11\u5143\u7d20\u7684\u90a3\u4e2a\u3002\n", "\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### > Loop variables leaking out!/\u5faa\u73af\u53d8\u91cf\u6cc4\u6f0f!\n", "1\\.\n" ] }, { "cell_type": "code", "metadata": { "collapsed": true }, "execution_count": null, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [] } ], "source": [ "for x in range(7):\n", " if x == 6:\n", " print(x, ': for x inside loop')\n", "print(x, ': x in global')\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "**Output:**\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "6 : for x inside loop\n", "6 : x in global\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "\u4f46\u662f `x` \u4ece\u672a\u5728\u5faa\u73af\u5916\u88ab\u5b9a\u4e49...\n", "\n", "2\\.\n" ] }, { "cell_type": "code", "metadata": { "collapsed": true }, "execution_count": null, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [] } ], "source": [ "# \u8fd9\u6b21\u6211\u4eec\u5148\u521d\u59cb\u5316x\n", "x = -1\n", "for x in range(7):\n", " if x == 6:\n", " print(x, ': for x inside loop')\n", "print(x, ': x in global')\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "**Output:**\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "6 : for x inside loop\n", "6 : x in global\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "3\\.\n", "```\n", "x = 1\n", "print([x for x in range(5)])\n", "print(x, ': x in global')\n", "```\n", "\n", "**Output (on Python 2.x):**\n", "```\n", "[0, 1, 2, 3, 4]\n", "(4, ': x in global')\n", "```\n", "\n", "**Output (on Python 3.x):**\n", "```\n", "[0, 1, 2, 3, 4]\n", "1 : x in global\n", "```\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### \ud83d\udca1 \u8bf4\u660e:\n", "\n", "- \u5728 Python \u4e2d, for \u5faa\u73af\u4f7f\u7528\u6240\u5728\u4f5c\u7528\u57df\u5e76\u5728\u7ed3\u675f\u540e\u4fdd\u7559\u5b9a\u4e49\u7684\u5faa\u73af\u53d8\u91cf. \u5982\u679c\u6211\u4eec\u66fe\u5728\u5168\u5c40\u547d\u540d\u7a7a\u95f4\u4e2d\u5b9a\u4e49\u8fc7\u5faa\u73af\u53d8\u91cf. \u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b, \u5b83\u4f1a\u91cd\u65b0\u7ed1\u5b9a\u73b0\u6709\u53d8\u91cf.\n", "\n", "- Python 2.x \u548c Python 3.x \u89e3\u91ca\u5668\u5728\u5217\u8868\u63a8\u5bfc\u5f0f\u793a\u4f8b\u4e2d\u7684\u8f93\u51fa\u5dee\u5f02, \u5728\u6587\u6863 [What\u2019s New In Python 3.0](https://docs.python.org/3/whatsnew/3.0.html) \u4e2d\u53ef\u4ee5\u627e\u5230\u76f8\u5173\u7684\u89e3\u91ca:\n", "\n", " > \"\u5217\u8868\u63a8\u5bfc\u4e0d\u518d\u652f\u6301\u53e5\u6cd5\u5f62\u5f0f `[... for var in item1, item2, ...]`. \u53d6\u800c\u4ee3\u4e4b\u7684\u662f `[... for var in (item1, item2, ...)]`. \u53e6\u5916, \u6ce8\u610f\u5217\u8868\u63a8\u5bfc\u5177\u6709\u4e0d\u540c\u7684\u8bed\u4e49: \u5b83\u4eec\u66f4\u63a5\u8fd1\u4e8e `list()` \u6784\u9020\u51fd\u6570\u4e2d\u751f\u6210\u5668\u8868\u8fbe\u5f0f\u7684\u8bed\u6cd5\u7cd6(\u8bd1: \u8fd9\u4e00\u53e5\u6211\u4e5f\u4e0d\u662f\u5f88\u660e\u767d), \u7279\u522b\u662f\u5faa\u73af\u63a7\u5236\u53d8\u91cf\u4e0d\u518d\u6cc4\u6f0f\u5230\u5468\u56f4\u7684\u4f5c\u7528\u57df\u4e2d.\"\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### > Beware of default mutable arguments!/\u5f53\u5fc3\u9ed8\u8ba4\u7684\u53ef\u53d8\u53c2\u6570!\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "def some_func(default_arg=[]):\n", " default_arg.append(\"some_string\")\n", " return default_arg\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "**Output:**\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "['some_string']\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "some_func()\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "['some_string', 'some_string']\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "some_func()\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "['some_string']\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "some_func([])\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "['some_string', 'some_string', 'some_string']\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "some_func()\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### \ud83d\udca1 \u8bf4\u660e:\n", "\n", "- Python\u4e2d\u51fd\u6570\u7684\u9ed8\u8ba4\u53ef\u53d8\u53c2\u6570\u5e76\u4e0d\u662f\u6bcf\u6b21\u8c03\u7528\u8be5\u51fd\u6570\u65f6\u90fd\u4f1a\u88ab\u521d\u59cb\u5316. \u76f8\u53cd, \u5b83\u4eec\u4f1a\u4f7f\u7528\u6700\u8fd1\u5206\u914d\u7684\u503c\u4f5c\u4e3a\u9ed8\u8ba4\u503c. \u5f53\u6211\u4eec\u660e\u786e\u7684\u5c06 `[]` \u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9 `some_func` \u7684\u65f6\u5019, \u5c31\u4e0d\u4f1a\u4f7f\u7528 `default_arg` \u7684\u9ed8\u8ba4\u503c, \u6240\u4ee5\u51fd\u6570\u4f1a\u8fd4\u56de\u6211\u4eec\u6240\u671f\u671b\u7684\u7ed3\u679c.\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ " def some_func(default_arg=[]):\n", " default_arg.append(\"some_string\")\n", " return default_arg\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", " **Output:**\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ " ([],)\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ " some_func.__defaults__ # \u8fd9\u91cc\u4f1a\u663e\u793a\u51fd\u6570\u7684\u9ed8\u8ba4\u53c2\u6570\u7684\u503c\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ " (['some_string'],)\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ " some_func()\n", " some_func.__defaults__\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ " (['some_string', 'some_string'],)\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ " some_func()\n", " some_func.__defaults__\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ " (['some_string', 'some_string'],)\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ " some_func([])\n", " some_func.__defaults__\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "- \u907f\u514d\u53ef\u53d8\u53c2\u6570\u5bfc\u81f4\u7684\u9519\u8bef\u7684\u5e38\u89c1\u505a\u6cd5\u662f\u5c06 `None` \u6307\u5b9a\u4e3a\u53c2\u6570\u7684\u9ed8\u8ba4\u503c, \u7136\u540e\u68c0\u67e5\u662f\u5426\u6709\u503c\u4f20\u7ed9\u5bf9\u5e94\u7684\u53c2\u6570. \u4f8b:\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ " def some_func(default_arg=None):\n", " if not default_arg:\n", " default_arg = []\n", " default_arg.append(\"some_string\")\n", " return default_arg\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### > Catching the Exceptions/\u6355\u83b7\u5f02\u5e38\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "some_list = [1, 2, 3]\n", "try:\n", " # \u8fd9\u91cc\u4f1a\u629b\u51fa\u5f02\u5e38 ``IndexError``\n", " print(some_list[4])\n", "except IndexError, ValueError:\n", " print(\"Caught!\")\n", "\n", "try:\n", " # \u8fd9\u91cc\u4f1a\u629b\u51fa\u5f02\u5e38 ``ValueError``\n", " some_list.remove(4)\n", "except IndexError, ValueError:\n", " print(\"Caught again!\")\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "**Output (Python 2.x):**\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "Caught!\n", "\n", "ValueError: list.remove(x): x not in list\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "**Output (Python 3.x):**\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ " File \"\", line 3\n", " except IndexError, ValueError:\n", " ^\n", "SyntaxError: invalid syntax\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### \ud83d\udca1 \u8bf4\u660e:\n", "\n", "* \u5982\u679c\u4f60\u60f3\u8981\u540c\u65f6\u6355\u83b7\u591a\u4e2a\u4e0d\u540c\u7c7b\u578b\u7684\u5f02\u5e38\u65f6, \u4f60\u9700\u8981\u5c06\u5b83\u4eec\u7528\u62ec\u53f7\u5305\u6210\u4e00\u4e2a\u5143\u7ec4\u4f5c\u4e3a\u7b2c\u4e00\u4e2a\u53c2\u6570\u4f20\u9012. \u7b2c\u4e8c\u4e2a\u53c2\u6570\u662f\u53ef\u9009\u540d\u79f0, \u5982\u679c\u4f60\u63d0\u4f9b, \u5b83\u5c06\u4e0e\u88ab\u6355\u83b7\u7684\u5f02\u5e38\u5b9e\u4f8b\u7ed1\u5b9a. \u4f8b,\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ " some_list = [1, 2, 3]\n", " try:\n", " # \u8fd9\u91cc\u4f1a\u629b\u51fa\u5f02\u5e38 ``ValueError``\n", " some_list.remove(4)\n", " except (IndexError, ValueError), e:\n", " print(\"Caught again!\")\n", " print(e)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " **Output (Python 2.x):**\n", " ```\n", " Caught again!\n", " list.remove(x): x not in list\n", " ```\n", " **Output (Python 3.x):**\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ " File \"\", line 4\n", " except (IndexError, ValueError), e:\n", " ^\n", " IndentationError: unindent does not match any outer indentation level\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "* \u5728 Python 3 \u4e2d, \u7528\u9017\u53f7\u533a\u5206\u5f02\u5e38\u4e0e\u53ef\u9009\u540d\u79f0\u662f\u65e0\u6548\u7684; \u6b63\u786e\u7684\u505a\u6cd5\u662f\u4f7f\u7528 `as` \u5173\u952e\u5b57. \u4f8b,\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ " some_list = [1, 2, 3]\n", " try:\n", " some_list.remove(4)\n", "\n", " except (IndexError, ValueError) as e:\n", " print(\"Caught again!\")\n", " print(e)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " **Output:**\n", " ```\n", " Caught again!\n", " list.remove(x): x not in list\n", " ```\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### > Same operands, different story!/\u540c\u4eba\u4e0d\u540c\u547d!\n", "1\\.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "a = [1, 2, 3, 4]\n", "b = a\n", "a = a + [5, 6, 7, 8]\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "**Output:**\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 4, 5, 6, 7, 8]\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "a\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 4]\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "b\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "2\\.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "a = [1, 2, 3, 4]\n", "b = a\n", "a += [5, 6, 7, 8]\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "**Output:**\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 4, 5, 6, 7, 8]\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "a\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 4, 5, 6, 7, 8]\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "b\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### \ud83d\udca1 \u8bf4\u660e:\n", "\n", "* `a += b` \u5e76\u4e0d\u603b\u662f\u4e0e `a = a + b` \u8868\u73b0\u76f8\u540c. \u7c7b\u5b9e\u73b0 *`op=`* \u8fd0\u7b97\u7b26\u7684\u65b9\u5f0f *\u4e5f\u8bb8* \u662f\u4e0d\u540c\u7684, \u5217\u8868\u5c31\u662f\u8fd9\u6837\u505a\u7684.\n", "\n", "* \u8868\u8fbe\u5f0f `a = a + [5,6,7,8]` \u4f1a\u751f\u6210\u4e00\u4e2a\u65b0\u5217\u8868, \u5e76\u8ba9 `a` \u5f15\u7528\u8fd9\u4e2a\u65b0\u5217\u8868, \u540c\u65f6\u4fdd\u6301 `b` \u4e0d\u53d8.\n", "\n", "* \u8868\u8fbe\u5f0f `a += [5,6,7,8]` \u5b9e\u9645\u4e0a\u662f\u4f7f\u7528\u7684\u662f \"extend\" \u51fd\u6570, \u6240\u4ee5 `a` \u548c `b` \u4ecd\u7136\u6307\u5411\u5df2\u88ab\u4fee\u6539\u7684\u540c\u4e00\u5217\u8868.\n", "\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### > Name resolution ignoring class scope/\u5ffd\u7565\u7c7b\u4f5c\u7528\u57df\u7684\u540d\u79f0\u89e3\u6790\n", "1\\.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "x = 5\n", "class SomeClass:\n", " x = 17\n", " y = (x for i in range(10))\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "**Output:**\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "5\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "list(SomeClass.y)[0]\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "2\\.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "x = 5\n", "class SomeClass:\n", " x = 17\n", " y = [x for i in range(10)]\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "**Output (Python 2.x):**\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "17\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "SomeClass.y[0]\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "**Output (Python 3.x):**\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "5\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "SomeClass.y[0]\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### \ud83d\udca1 \u8bf4\u660e:\n", "- \u7c7b\u5b9a\u4e49\u4e2d\u5d4c\u5957\u7684\u4f5c\u7528\u57df\u4f1a\u5ffd\u7565\u7c7b\u5185\u7684\u540d\u79f0\u7ed1\u5b9a.\n", "- \u751f\u6210\u5668\u8868\u8fbe\u5f0f\u6709\u5b83\u81ea\u5df1\u7684\u4f5c\u7528\u57df.\n", "- \u4ece Python 3.X \u5f00\u59cb, \u5217\u8868\u63a8\u5bfc\u5f0f\u4e5f\u6709\u81ea\u5df1\u7684\u4f5c\u7528\u57df.\n", "\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### > Rounding like a banker/\u50cf\u94f6\u884c\u5bb6\u4e00\u6837\u820d\u5165 *\n", "\u8ba9\u6211\u4eec\u5b9e\u73b0\u4e00\u4e2a\u7b80\u5355\u7684\u51fd\u6570\u6765\u83b7\u53d6\u5217\u8868\u7684\u4e2d\u95f4\u5143\u7d20\uff1a\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "def get_middle(some_list):\n", " mid_index = round(len(some_list) / 2)\n", " return some_list[mid_index - 1]\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "**Python 3.x:**\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "1\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "get_middle([1]) # looks good\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "2\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "get_middle([1,2,3]) # looks good\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "2\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "get_middle([1,2,3,4,5]) # huh?\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "2.5\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "len([1,2,3,4,5]) / 2 # good\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "2\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "round(len([1,2,3,4,5]) / 2) # why?\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "\u4f3c\u4e4e Python \u5c06 2.5 \u820d\u5165\u5230 2\u3002\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### \ud83d\udca1 \u8bf4\u660e\n", "\n", "- - \u8fd9\u4e0d\u662f\u6d6e\u70b9\u7cbe\u5ea6\u9519\u8bef\uff0c\u5b9e\u9645\u4e0a\uff0c\u8fd9\u79cd\u884c\u4e3a\u662f\u6545\u610f\u7684\u3002\u4ece Python 3.0 \u5f00\u59cb\uff0c`round()` \u4f7f\u7528[\u94f6\u884c\u8fdb\u4f4d\u6cd5](https://en.wikipedia.org/wiki/Rounding#Round_half_to_even)\uff0c\u5176\u4e2d 0.5 \u5c0f\u6570\u56db\u820d\u4e94\u5165\u5230\u6700\u63a5\u8fd1\u7684 **\u5076\u6570** \uff1a\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "0\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "round(0.5)\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "2\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "round(1.5)\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "2\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "round(2.5)\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "0.0\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "import numpy # numpy\u7684\u7ed3\u679c\u4e5f\u662f\u4e00\u6837\n", "numpy.round(0.5)\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "2.0\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "numpy.round(1.5)\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "2.0\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "numpy.round(2.5)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "- \u8fd9\u662f [IEEE 754](https://en.wikipedia.org/wiki/IEEE_754#Rounding_rules) \u4e2d\u63cf\u8ff0\u7684\u5173\u4e8e0.5\u5206\u4f4d\u820d\u5165\u7684\u63a8\u8350\u65b9\u6cd5\u3002\u7136\u800c\uff0c\u53e6\u4e00\u79cd\u65b9\u6cd5\uff08\u4ece\u96f6\u53d6\u6574\uff09\u5927\u90e8\u5206\u65f6\u95f4\u90fd\u662f\u5728\u5b66\u6821\u6559\u6388\u7684\uff0c\u6240\u4ee5\u94f6\u884c\u8fdb\u4f4d\u6cd5\u53ef\u80fd\u5e76\u4e0d\u4e3a\u4eba\u6240\u77e5\u3002\u6b64\u5916\uff0c\u4e00\u4e9b\u6700\u6d41\u884c\u7684\u7f16\u7a0b\u8bed\u8a00\uff08\u4f8b\u5982\uff1aJavaScript\u3001Java\u3001C/C++\u3001Ruby\u3001Rust\uff09\u4e5f\u4e0d\u4f7f\u7528\u94f6\u884c\u8fdb\u4f4d\u6cd5\u3002\u56e0\u6b64\uff0c\u8fd9\u5bf9 Python \u6765\u8bf4\u8fd8\u662f\u6bd4\u8f83\u7279\u6b8a\u7684\uff0c\u5728\u56db\u820d\u4e94\u5165\u65f6\u53ef\u80fd\u4f1a\u5bfc\u81f4\u6df7\u6dc6\u3002\n", "- \u4e86\u89e3\u66f4\u591a\u4fe1\u606f\uff0c\u8bf7\u53c2\u9605\u6587\u6863 [round()](https://docs.python.org/3/library/functions.html#round) \u6216 [this stackoverflow thread](https://stackoverflow.com/questions/10825926/python -3-x-rounding-behavior) \n", "- \u8bf7\u6ce8\u610f\uff0c`get_middle([1])` \u53ea\u8fd4\u56de1\uff0c\u56e0\u4e3a\u5b83\u7684\u7d22\u5f15\u662f `round(0.5) - 1 = 0 - 1 = -1`\uff0c\u8fd4\u56de\u5217\u8868\u4e2d\u7684\u6700\u540e\u4e00\u4e2a\u5143\u7d20\u3002\n", "\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### > Needles in a Haystack/\u5927\u6d77\u635e\u9488\n", "\u8fc4\u4eca\u4e3a\u6b62\uff0c\u6bcf\u4e00\u4f4dPython\u5f00\u53d1\u8005\u90fd\u4f1a\u9047\u5230\u7c7b\u4f3c\u4ee5\u4e0b\u7684\u60c5\u51b5\u3002\n", "\n", "1\\.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "x, y = (0, 1) if True else None, None\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "**Output:**\n", "```\n", ">>> x, y # \u671f\u671b\u7684\u7ed3\u679c\u662f (0, 1)\n", "((0, 1), None)\n", "```\n", "\n", "2\\.\n" ] }, { "cell_type": "code", "metadata": { "collapsed": true }, "execution_count": null, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [] } ], "source": [ "t = ('one', 'two')\n", "for i in t:\n", " print(i)\n", "\n", "t = ('one')\n", "for i in t:\n", " print(i)\n", "\n", "t = ()\n", "print(t)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "**Output:**\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "one\n", "two\n", "o\n", "n\n", "e\n", "tuple()\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "3\\.\n", "\n", "```\n", "ten_words_list = [\n", " \"some\",\n", " \"very\",\n", " \"big\",\n", " \"list\",\n", " \"that\"\n", " \"consists\",\n", " \"of\",\n", " \"exactly\",\n", " \"ten\",\n", " \"words\"\n", "]\n", "```\n", "\n", "**Output**\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "9\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "len(ten_words_list)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "4\\. \u4e0d\u591f\u5065\u58ee\u7684\u65ad\u8a00\u673a\u5236\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "a = \"python\"\n", "b = \"javascript\"\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "**Output:**\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "# \u672a\u5f15\u53d1 AssertionError \n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "# \u5e26\u6709\u5931\u8d25\u8b66\u544a\u4fe1\u606f\u7684assert\u8868\u8fbe\u5f0f\n", "assert(a == b, \"Both languages are different\")\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "5\\.\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "some_list = [1, 2, 3]\n", "some_dict = {\n", " \"key_1\": 1,\n", " \"key_2\": 2,\n", " \"key_3\": 3\n", "}\n", "\n", "some_list = some_list.append(4) \n", "some_dict = some_dict.update({\"key_4\": 4})\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "**Output:**\n", "\n" ] }, { "cell_type": "code", "metadata": { "collapsed": true }, "execution_count": null, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "None\n" ] } ], "source": [ "print(some_list)\n" ] }, { "cell_type": "code", "metadata": { "collapsed": true }, "execution_count": null, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "None\n" ] } ], "source": [ "print(some_dict)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "6\\.\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "def some_recursive_func(a):\n", " if a[0] == 0:\n", " return\n", " a[0] -= 1\n", " some_recursive_func(a)\n", " return a\n", "\n", "def similar_recursive_func(a):\n", " if a == 0:\n", " return a\n", " a -= 1\n", " similar_recursive_func(a)\n", " return a\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "**Output:**\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "[0, 0]\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "some_recursive_func([5, 0])\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "4\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "similar_recursive_func(5)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### \ud83d\udca1 \u8bf4\u660e:\n", "* \u5bf9\u4e8e 1, \u6b63\u786e\u7684\u8bed\u53e5\u662f `x, y = (0, 1) if True else (None, None)`.\n", "* \u5bf9\u4e8e 2, \u6b63\u786e\u7684\u8bed\u53e5\u662f `t = ('one',)` \u6216\u8005 `t = 'one',` (\u7f3a\u5c11\u9017\u53f7) \u5426\u5219\u89e3\u91ca\u5668\u4f1a\u8ba4\u4e3a `t` \u662f\u4e00\u4e2a\u5b57\u7b26\u4e32, \u5e76\u9010\u4e2a\u5b57\u7b26\u5bf9\u5176\u8fdb\u884c\u8fed\u4ee3.\n", "* `()` \u662f\u4e00\u4e2a\u7279\u6b8a\u7684\u6807\u8bb0\uff0c\u8868\u793a\u7a7a\u5143\u7ec4.\n", "* \u5bf9\u4e8e 3\uff0c\u6b63\u5982\u60a8\u53ef\u80fd\u5df2\u7ecf\u5f04\u6e05\u695a\u7684\u90a3\u6837\uff0c\u5217\u8868\u4e2d\u7684\u7b2c5\u4e2a\u5143\u7d20\uff08\"that\"\uff09\u540e\u9762\u7f3a\u5c11\u4e00\u4e2a\u9017\u53f7\u3002\u56e0\u6b64\uff0c\u901a\u8fc7\u9690\u5f0f\u5b57\u7b26\u4e32\u5b57\u9762\u8fde\u63a5\uff0c\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ " ['some', 'very', 'big', 'list', 'thatconsists', 'of', 'exactly', 'ten', 'words']\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ " ten_words_list\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "* \u5728\u7b2c4\u4e2a\u4ee3\u7801\u6bb5\u4e2d\u6ca1\u6709\u5f15\u53d1\"AssertionError\"\uff0c\u56e0\u4e3a\u6211\u4eec\u4e0d\u662f\u65ad\u8a00\u5355\u4e2a\u8868\u8fbe\u5f0f `a == b`\uff0c\u800c\u662f\u65ad\u8a00\u6574\u4e2a\u5143\u7ec4\u3002\u4ee5\u4e0b\u4ee3\u7801\u6bb5\u5c06\u8bf4\u660e\u95ee\u9898\uff0c\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ " Traceback (most recent call last):\n", " File \"\", line 1, in \n", " AssertionError\n", " \n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ " a = \"python\"\n", " b = \"javascript\"\n", " assert a == b\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ " :1: SyntaxWarning: assertion is always true, perhaps remove parentheses?\n", " \n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ " assert (a == b, \"Values are not equal\")\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ " Traceback (most recent call last):\n", " File \"\", line 1, in \n", " AssertionError: Values are not equal\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ " assert a == b, \"Values are not equal\"\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "* \u81f3\u4e8e\u7b2c\u4e94\u4e2a\u7247\u6bb5\uff0c\u5927\u591a\u6570\u4fee\u6539\u5e8f\u5217/\u6620\u5c04\u5bf9\u8c61\u9879\u7684\u65b9\u6cd5\uff0c\u5982`list.append`\u3001`dict.update`\u3001`list.sort`\u7b49\uff0c\u90fd\u5728\u539f\u5730\u4fee\u6539\u5bf9\u8c61\u5e76\u8fd4\u56de`None`\u3002\u8fd9\u80cc\u540e\u7684\u57fa\u672c\u539f\u7406\u662f\u901a\u8fc7\u539f\u5730\u64cd\u4f5c\uff0c\u907f\u514d\u590d\u5236\u5bf9\u8c61\u6765\u63d0\u9ad8\u6027\u80fd(\u53c2\u8003[\u8fd9\u91cc](https://docs.python.org/3/faq/design.html#why-doesn-t-list-sort-return-the-sorted-list))\u3002\n", "* \u6700\u540e\u4e00\u4e2a\u5e94\u8be5\u76f8\u5f53\u660e\u663e\uff0c\u53ef\u53d8\u5bf9\u8c61\uff08\u5982`list`\uff09\u53ef\u4ee5\u5728\u51fd\u6570\u4e2d\u66f4\u6539\uff0c\u4e0d\u53ef\u53d8\u5bf9\u8c61\uff08`a -= 1`\uff09\u7684\u91cd\u65b0\u8d4b\u503c\u5219\u4e0d\u5c5e\u4e8e\u503c\u7684\u6539\u53d8\u3002\n", "* \u4e86\u89e3\u8fd9\u4e9b\u7ec6\u8282\u53ef\u4ee5\u5728\u7a0b\u5e8f\u957f\u671f\u8fd0\u884c\u4e2d\uff0c\u4e3a\u60a8\u8282\u7701\u6570\u5c0f\u65f6\u7684\u8c03\u8bd5\u5de5\u4f5c\u3002\n", "\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### > Splitsies/\u5206\u5272\u51fd\u6570 *\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "['a']\n", "\n", "# is same as\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "'a'.split()\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "['a']\n", "\n", "# but\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "'a'.split(' ')\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "0\n", "\n", "# isn't the same as\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "len(''.split())\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "1\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "len(''.split(' '))\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### \ud83d\udca1 \u8bf4\u660e\n", "\n", "- \u8d77\u521d\u4eba\u4eec\u53ef\u80fd\u4f1a\u8ba4\u4e3a split \u7684\u9ed8\u8ba4\u5206\u9694\u7b26\u662f\u5355\u4e2a\u7a7a\u683c `' '`\uff0c\u4f46\u6839\u636e [\u6587\u6863](https://docs.python.org/3/library/stdtypes.html#str.split)\uff1a\n", " > \u5982\u679c sep \u672a\u6307\u5b9a\u6216\u4e3a `None`\uff0c\u5219\u5e94\u7528\u4e0d\u540c\u7684\u62c6\u5206\u7b97\u6cd5\uff1a\u8fde\u7eed\u7684\u7a7a\u683c\u88ab\u89c6\u4e3a\u5355\u4e2a\u5206\u9694\u7b26\uff0c\u5982\u679c\u5b57\u7b26\u4e32\u6709\u524d\u5bfc\u6216\u5c3e\u968f\u7a7a\u683c\uff0c\u5219\u7ed3\u679c\u5c06\u5728\u5f00\u5934\u6216\u7ed3\u5c3e\u4e0d\u5305\u542b\u7a7a\u5b57\u7b26\u4e32\u3002\u56e0\u6b64\uff0c\u4f7f\u7528 `None` \u5206\u9694\u7b26\u62c6\u5206\u7a7a\u5b57\u7b26\u4e32\u6216\u4ec5\u5305\u542b\u7a7a\u683c\u7684\u5b57\u7b26\u4e32\u5c06\u8fd4\u56de `[]`\u3002\n", " > \u5982\u679c\u7ed9\u5b9a sep\uff0c\u8fde\u7eed\u7684\u5206\u9694\u7b26\u4e0d\u4f1a\u7ec4\u5408\u5728\u4e00\u8d77\uff0c\u5e76\u88ab\u89c6\u4e3a\u5206\u9694\u7a7a\u5b57\u7b26\u4e32\uff08\u4f8b\u5982\uff0c`'1,,2'.split(',')` \u8fd4\u56de `['1', '', '2 ']`\uff09\u3002\u4f7f\u7528\u6307\u5b9a\u7684\u5206\u9694\u7b26\u62c6\u5206\u7a7a\u5b57\u7b26\u4e32\u4f1a\u8fd4\u56de `['']`\u3002\n", "- Noticing how the leading and trailing whitespaces are handled in the following snippet will make things clear,\n", "- \u6ce8\u610f\u4ee5\u4e0b\u4ee3\u7801\u6bb5\u4e2d\u5982\u4f55\u5904\u7406\u524d\u5bfc\u548c\u5c3e\u968f\u7a7a\u683c\uff0c\u4fc3\u8fdb\u66f4\u6df1\u5165\u7684\u7406\u89e3\uff1a\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ " ['', 'a', '']\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ " ' a '.split(' ')\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ " ['a']\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ " ' a '.split()\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ " ['']\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ " ''.split(' ')\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### > All sorted?/\u90fd\u6392\u5e8f\u4e86\u5417\uff1f *\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "False\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "x = 7, 8, 9\n", "sorted(x) == x\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "True\n", "\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "sorted(x) == sorted(x)\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "False\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "y = reversed(x)\n", "sorted(y) == sorted(y)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### \ud83d\udca1 \u8bf4\u660e\n", "\n", "- `sorted` \u65b9\u6cd5\u4e00\u5b9a\u8fd4\u56de\u5217\u8868\u7c7b\u578b, \u6bd4\u8f83\u5217\u8868\u4e0e\u5143\u7ec4\u5728Python\u4e2d\u4e00\u5b9a\u8fd4\u56de `False`. \n", "\n", "- ```py\n", " >>> [] == tuple()\n", " False\n", " >>> x = 7, 8, 9\n", " >>> type(x), type(sorted(x))\n", " (tuple, list)\n", " ```\n", "\n", "- \u4e0e `sorted` \u4e0d\u540c\uff0c`reversed` \u65b9\u6cd5\u8fd4\u56de\u4e00\u4e2a\u8fed\u4ee3\u5668\u3002\u4e3a\u4ec0\u4e48\uff1f\u56e0\u4e3a\u6392\u5e8f\u9700\u8981\u5c31\u5730\u4fee\u6539\u8fed\u4ee3\u5668\u6216\u4f7f\u7528\u989d\u5916\u7684\u5bb9\u5668\uff08\u5217\u8868\uff09\uff0c\u800c\u53cd\u5411\u53ef\u4ee5\u7b80\u5355\u5730\u901a\u8fc7\u4ece\u6700\u540e\u4e00\u4e2a\u7d22\u5f15\u8fed\u4ee3\u5230\u7b2c\u4e00\u4e2a\u7d22\u5f15\u6765\u5de5\u4f5c\u3002\n", "\n", "- \u6240\u4ee5\u5728\u6bd4\u8f83 `sorted(y) == sorted(y)` \u65f6\uff0c\u7b2c\u4e00\u6b21\u8c03\u7528 `sorted()` \u4f1a\u6d88\u8017\u8fed\u4ee3\u5668 `y`\uff0c\u4e0b\u4e00\u6b21\u8c03\u7528\u53ea\u4f1a\u8fd4\u56de\u4e00\u4e2a\u7a7a\u5217\u8868\u3002\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ " ([7, 8, 9], [])\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ " x = 7, 8, 9\n", " y = reversed(x)\n", " sorted(y), sorted(y)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### > Midnight time doesn't exist?/\u4e0d\u5b58\u5728\u7684\u5348\u591c?\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "from datetime import datetime\n", "\n", "midnight = datetime(2018, 1, 1, 0, 0)\n", "midnight_time = midnight.time()\n", "\n", "noon = datetime(2018, 1, 1, 12, 0)\n", "noon_time = noon.time()\n", "\n", "if midnight_time:\n", " print(\"Time at midnight is\", midnight_time)\n", "\n", "if noon_time:\n", " print(\"Time at noon is\", noon_time)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "**Output:**\n", "```sh\n", "('Time at noon is', datetime.time(12, 0))\n", "```\n", "\n", "midnight_time \u5e76\u6ca1\u6709\u88ab\u8f93\u51fa.\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### \ud83d\udca1 \u8bf4\u660e:\n", "\n", "\u5728Python 3.5\u4e4b\u524d, \u5982\u679c `datetime.time` \u5bf9\u8c61\u5b58\u50a8\u7684UTC\u7684\u5348\u591c\u65f6\u95f4(\u8bd1: \u5c31\u662f `00:00`), \u90a3\u4e48\u5b83\u7684\u5e03\u5c14\u503c\u4f1a\u88ab\u8ba4\u4e3a\u662f `False`. \u5f53\u4f7f\u7528 `if obj:` \u8bed\u53e5\u6765\u68c0\u67e5 `obj` \u662f\u5426\u4e3a `null` \u6216\u8005\u67d0\u4e9b\u201c\u7a7a\u201d\u503c\u7684\u65f6\u5019, \u5f88\u5bb9\u6613\u51fa\u9519.\n", "\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### > Okay Python, Can you make me fly?/Python, \u53ef\u5426\u5e26\u6211\u98de? *\n", "\u597d, \u53bb\u5427.\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "import antigravity\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "**Output:**\n", "\u5618.. \u8fd9\u662f\u4e2a\u8d85\u7ea7\u79d8\u5bc6.\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### \ud83d\udca1 \u8bf4\u660e:\n", "+ `antigravity` \u6a21\u5757\u662f Python \u5f00\u53d1\u4eba\u5458\u53d1\u5e03\u7684\u5c11\u6570\u590d\u6d3b\u8282\u5f69\u86cb\u4e4b\u4e00.\n", "+ `import antigravity` \u4f1a\u6253\u5f00\u4e00\u4e2a Python \u7684[\u7ecf\u5178 XKCD \u6f2b\u753b](http://xkcd.com/353/)\u9875\u9762.\n", "+ \u4e0d\u6b62\u5982\u6b64. \u8fd9\u4e2a**\u590d\u6d3b\u8282\u5f69\u86cb\u91cc\u8fd8\u6709\u4e00\u4e2a\u590d\u6d3b\u8282\u5f69\u86cb**. \u5982\u679c\u4f60\u770b\u4e00\u4e0b[\u4ee3\u7801](https://github.com/python/cpython/blob/master/Lib/antigravity.py#L7-L17), \u5c31\u4f1a\u53d1\u73b0\u8fd8\u6709\u4e00\u4e2a\u51fd\u6570\u5b9e\u73b0\u4e86 [XKCD's geohashing \u7b97\u6cd5](https://xkcd.com/426/).\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### > `goto`, but why?/`goto`, \u4f46\u4e3a\u4ec0\u4e48? *\n" ] }, { "cell_type": "code", "metadata": { "collapsed": true }, "execution_count": null, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [] } ], "source": [ "from goto import goto, label\n", "for i in range(9):\n", " for j in range(9):\n", " for k in range(9):\n", " print(\"I'm trapped, please rescue!\")\n", " if k == 2:\n", " goto .breakout # \u4ece\u591a\u91cd\u5faa\u73af\u4e2d\u8df3\u51fa\n", "label .breakout\n", "print(\"Freedom!\")\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "**Output (Python 2.3):**\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "I'm trapped, please rescue!\n", "I'm trapped, please rescue!\n", "Freedom!\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### \ud83d\udca1 \u8bf4\u660e:\n", "- 2004\u5e744\u67081\u65e5, Python [\u5ba3\u5e03](https://mail.python.org/pipermail/python-announce-list/2004-April/002982.html) \u52a0\u5165\u4e00\u4e2a\u53ef\u7528\u7684 `goto` \u4f5c\u4e3a\u611a\u4eba\u8282\u793c\u7269.\n", "- \u5f53\u524d\u7248\u672c\u7684 Python \u5e76\u6ca1\u6709\u8fd9\u4e2a\u6a21\u5757.\n", "- \u5c31\u7b97\u53ef\u4ee5\u7528, \u4e5f\u8bf7\u4e0d\u8981\u4f7f\u7528\u5b83. \u8fd9\u91cc\u662f\u4e3a\u4ec0\u4e48Python\u4e2d\u6ca1\u6709 `goto` \u7684[\u539f\u56e0](https://docs.python.org/3/faq/design.html#why-is-there-no-goto).\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### > Brace yourself!/\u505a\u597d\u601d\u60f3\u51c6\u5907 *\n", "\u5982\u679c\u4f60\u4e0d\u559c\u6b22\u5728Python\u4e2d\u4f7f\u7528\u7a7a\u683c\u6765\u8868\u793a\u4f5c\u7528\u57df, \u4f60\u53ef\u4ee5\u5bfc\u5165 C \u98ce\u683c\u7684 {},\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "from __future__ import braces\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "**Output:**\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ " File \"some_file.py\", line 1\n", " from __future__ import braces\n", "SyntaxError: not a chance\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "\u60f3\u7528\u5927\u62ec\u53f7 `braces`? \u6ca1\u95e8! \u89c9\u5f97\u4e0d\u723d, \u8bf7\u53bb\u7528java\u3002\u90a3\u4e48\uff0c\u53e6\u4e00\u4e2a\u4ee4\u4eba\u60ca\u8bb6\u7684\u4e8b\u60c5\uff0c\u627e\u4e00\u627e\u5728 `__future__` \u6a21\u5757\u4e2d\uff0c\u54ea\u91cc\u5f15\u53d1\u4e86 `SyntaxError` [code](https://github.com/python/cpython/blob/master/Lib/__future__.py)?\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### \ud83d\udca1 \u8bf4\u660e:\n", "+ \u901a\u5e38 `__future__` \u4f1a\u63d0\u4f9b Python \u672a\u6765\u7248\u672c\u7684\u529f\u80fd. \u7136\u800c\uff0c\u8fd9\u91cc\u7684 \u201c\u672a\u6765\u201d \u662f\u4e00\u4e2a\u8bbd\u523a.\n", "+ \u8fd9\u662f\u4e00\u4e2a\u8868\u8fbe\u793e\u533a\u5bf9\u6b64\u7c7b\u95ee\u9898\u6001\u5ea6\u7684\u590d\u6d3b\u8282\u5f69\u86cb.\n", "+ \u4ee3\u7801\u5b9e\u9645\u4e0a\u5728[`future.c` \u6587\u4ef6]\u4e2d (https://github.com/python/cpython/blob/025eb98dc0c1dc27404df6c544fc2944e0fa9f3a/Python/future.c#L49).\n", "+ \u5f53 CPython \u7f16\u8bd1\u5668\u9047\u5230 [future\u8868\u8fbe\u5f0f](https://docs.python.org/3.3/reference/simple_stmts.html#future-statements) \u65f6\uff0c\u5b83\u9996\u5148\u5728 `future.c` \u4e2d\u8fd0\u884c\u76f8\u5e94\u7684\u4ee3\u7801\uff0c\u7136\u540e\u518d\u5bf9\u5176\u8fdb\u884c\u5904\u7406\u4f5c\u4e3a\u6b63\u5e38\u7684`import`\u8868\u8fbe\u5f0f\u3002\n", "\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### > Let's meet Friendly Language Uncle For Life/\u8ba9\u751f\u6d3b\u66f4\u53cb\u597d *\n", "**Output (Python 3.x)**\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ " File \"some_file.py\", line 1\n", " \"Ruby\" != \"Python\"\n", " ^\n", "SyntaxError: invalid syntax\n", "\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "from __future__ import barry_as_FLUFL\n", "\"Ruby\" != \"Python\" # \u8fd9\u91cc\u6ca1\u4ec0\u4e48\u7591\u95ee\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "True\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "\"Ruby\" <> \"Python\"\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "\u8fd9\u5c31\u5bf9\u4e86.\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### \ud83d\udca1 \u8bf4\u660e:\n", "- \u76f8\u5173\u7684 [PEP-401](https://www.python.org/dev/peps/pep-0401/) \u53d1\u5e03\u4e8e 2009\u5e744\u67081\u65e5 (\u6240\u4ee5\u4f60\u73b0\u5728\u77e5\u9053\u8fd9\u610f\u5473\u7740\u4ec0\u4e48\u4e86\u5427).\n", "- \u5f15\u7528 PEP-401\n", " > \u610f\u8bc6\u5230 Python 3.0 \u91cc\u7684 != \u8fd0\u7b97\u7b26\u662f\u4e00\u4e2a\u4f1a\u5f15\u8d77\u624b\u6307\u75bc\u75db\u7684\u6050\u6016\u9519\u8bef, FLUFL \u5c06 <> \u8fd0\u7b97\u7b26\u6062\u590d\u4e3a\u552f\u4e00\u5199\u6cd5.\n", "- Uncle Barry \u5728 PEP \u4e2d\u8fd8\u5206\u4eab\u4e86\u5176\u4ed6\u4e1c\u897f; \u4f60\u53ef\u4ee5\u5728[\u8fd9\u91cc](https://www.python.org/dev/peps/pep-0401/)\u83b7\u5f97\u4ed6\u4eec.\n", "- (\u8bd1: \u867d\u7136\u6587\u6863\u4e2d\u6ca1\u5199\uff0c\u4f46\u5e94\u8be5\u662f\u53ea\u80fd\u5728\u4ea4\u4e92\u89e3\u91ca\u5668\u4e2d\u4f7f\u7528.)\n", "- \u5b83\u5728\u4ea4\u4e92\u5f0f\u73af\u5883\u4e2d\u6b63\u5e38\u8fd0\u884c\uff0c\u4f46\u662f\u5f53\u60a8\u901a\u8fc7 python \u6587\u4ef6\u8fd0\u884c\u65f6\u5b83\u4f1a\u5f15\u53d1 `SyntaxError`\uff08\u8bf7\u53c2\u9605\u6b64 [\u95ee\u9898](https://github.com/satwikkansal/wtfpython/issues/94)\uff09\u3002\u60a8\u53ef\u4ee5\u5c06\u8868\u8fbe\u5f0f\u4f5c\u4e3a`eval` \u6216 `compile` \u53c2\u6570\u4e2d\u4f7f\u7528\u3002\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ " from __future__ import barry_as_FLUFL\n", " print(eval('\"Ruby\" <> \"Python\"'))\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### > Even Python understands that love is complicated/\u8fdePython\u4e5f\u77e5\u9053\u7231\u662f\u96be\u8a00\u7684 *\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "import this\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "\u7b49\u7b49, **this** \u662f\u4ec0\u4e48? `this` \u662f\u7231 :heart:\n", "\n", "**Output:**\n", "```\n", "The Zen of Python, by Tim Peters\n", "\n", "Beautiful is better than ugly.\n", "\u4f18\u7f8e\u80dc\u4e8e\u4e11\u964b\uff08Python \u4ee5\u7f16\u5199\u4f18\u7f8e\u7684\u4ee3\u7801\u4e3a\u76ee\u6807\uff09\n", "Explicit is better than implicit.\n", "\u660e\u4e86\u80dc\u4e8e\u6666\u6da9\uff08\u4f18\u7f8e\u7684\u4ee3\u7801\u5e94\u5f53\u662f\u660e\u4e86\u7684\uff0c\u547d\u540d\u89c4\u8303\uff0c\u98ce\u683c\u76f8\u4f3c\uff09\n", "Simple is better than complex.\n", "\u7b80\u6d01\u80dc\u4e8e\u590d\u6742\uff08\u4f18\u7f8e\u7684\u4ee3\u7801\u5e94\u5f53\u662f\u7b80\u6d01\u7684\uff0c\u4e0d\u8981\u6709\u590d\u6742\u7684\u5185\u90e8\u5b9e\u73b0\uff09\n", "Complex is better than complicated.\n", "\u590d\u6742\u80dc\u4e8e\u51cc\u4e71\uff08\u5982\u679c\u590d\u6742\u4e0d\u53ef\u907f\u514d\uff0c\u90a3\u4ee3\u7801\u95f4\u4e5f\u4e0d\u80fd\u6709\u96be\u61c2\u7684\u5173\u7cfb\uff0c\u8981\u4fdd\u6301\u63a5\u53e3\u7b80\u6d01\uff09\n", "Flat is better than nested.\n", "\u6241\u5e73\u80dc\u4e8e\u5d4c\u5957\uff08\u4f18\u7f8e\u7684\u4ee3\u7801\u5e94\u5f53\u662f\u6241\u5e73\u7684\uff0c\u4e0d\u80fd\u6709\u592a\u591a\u7684\u5d4c\u5957\uff09\n", "Sparse is better than dense.\n", "\u95f4\u9694\u80dc\u4e8e\u7d27\u51d1\uff08\u4f18\u7f8e\u7684\u4ee3\u7801\u6709\u9002\u5f53\u7684\u95f4\u9694\uff0c\u4e0d\u8981\u5962\u671b\u4e00\u884c\u4ee3\u7801\u89e3\u51b3\u95ee\u9898\uff09\n", "Readability counts.\n", "\u53ef\u8bfb\u6027\u5f88\u91cd\u8981\uff08\u4f18\u7f8e\u7684\u4ee3\u7801\u4e00\u5b9a\u662f\u53ef\u8bfb\u7684\uff09\n", "Special cases aren't special enough to break the rules.\n", "\u6ca1\u6709\u7279\u4f8b\u7279\u6b8a\u5230\u9700\u8981\u8fdd\u80cc\u8fd9\u4e9b\u89c4\u5219\uff08\u8fd9\u4e9b\u89c4\u5219\u81f3\u9ad8\u65e0\u4e0a\uff09\n", "Although practicality beats purity.\n", "\u5c3d\u7ba1\u6211\u4eec\u66f4\u503e\u5411\u4e8e\u5b9e\u7528\u6027\n", "Errors should never pass silently.\n", "\u4e0d\u8981\u5b89\u9759\u7684\u5305\u5bb9\u6240\u6709\u9519\u8bef\n", "Unless explicitly silenced.\n", "\u9664\u975e\u4f60\u786e\u5b9a\u9700\u8981\u8fd9\u6837\u505a\uff08\u7cbe\u51c6\u5730\u6355\u83b7\u5f02\u5e38\uff0c\u4e0d\u5199 except:pass \u98ce\u683c\u7684\u4ee3\u7801\uff09\n", "In the face of ambiguity, refuse the temptation to guess.\n", "\u62d2\u7edd\u8bf1\u60d1\u4f60\u53bb\u731c\u6d4b\u7684\u66a7\u6627\u4e8b\u7269\n", "There should be one-- and preferably only one --obvious way to do it.\n", "\u800c\u662f\u5c3d\u91cf\u627e\u4e00\u79cd\uff0c\u6700\u597d\u662f\u552f\u4e00\u4e00\u79cd\u660e\u663e\u7684\u89e3\u51b3\u65b9\u6848\uff08\u5982\u679c\u4e0d\u786e\u5b9a\uff0c\u5c31\u7528\u7a77\u4e3e\u6cd5\uff09\n", "Although that way may not be obvious at first unless you're Dutch.\n", "\u867d\u7136\u8fd9\u5e76\u4e0d\u5bb9\u6613\uff0c\u56e0\u4e3a\u4f60\u4e0d\u662f Python \u4e4b\u7236\uff08\u8fd9\u91cc\u7684 Dutch \u662f\u6307 Guido \uff09\n", "Now is better than never.\n", "\u73b0\u5728\u884c\u52a8\u597d\u8fc7\u6c38\u8fdc\u4e0d\u884c\u52a8\n", "Although never is often better than *right* now.\n", "\u5c3d\u7ba1\u4e0d\u884c\u52a8\u8981\u597d\u8fc7\u9c81\u83bd\u884c\u52a8\n", "If the implementation is hard to explain, it's a bad idea.\n", "\u5982\u679c\u4f60\u65e0\u6cd5\u5411\u4eba\u63cf\u8ff0\u4f60\u7684\u65b9\u6848\uff0c\u90a3\u80af\u5b9a\u4e0d\u662f\u4e00\u4e2a\u597d\u65b9\u6848\uff1b\n", "If the implementation is easy to explain, it may be a good idea.\n", "\u5982\u679c\u4f60\u80fd\u8f7b\u677e\u5411\u4eba\u63cf\u8ff0\u4f60\u7684\u65b9\u6848\uff0c\u90a3\u4e5f\u8bb8\u4f1a\u662f\u4e00\u4e2a\u597d\u65b9\u6848\uff08\u65b9\u6848\u6d4b\u8bc4\u6807\u51c6\uff09\n", "Namespaces are one honking great idea -- let's do more of those!\n", "\u547d\u540d\u7a7a\u95f4\u662f\u4e00\u79cd\u7edd\u5999\u7684\u7406\u5ff5\uff0c\u6211\u4eec\u5e94\u5f53\u591a\u52a0\u5229\u7528\uff08\u5021\u5bfc\u4e0e\u53f7\u53ec\uff09\n", "```\n", "\n", "\u8fd9\u662f Python \u4e4b\u7985!\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "True\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "love = this\n", "this is love\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "False\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "love is True\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "False\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "love is False\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "True\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "love is not True or False\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "True\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "love is not True or False; love is love # \u7231\u662f\u96be\u8a00\u7684\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### \ud83d\udca1 \u8bf4\u660e:\n", "\n", "* `this` \u6a21\u5757\u662f\u5173\u4e8e Python \u4e4b\u7985\u7684\u590d\u6d3b\u8282\u5f69\u86cb ([PEP 20](https://www.python.org/dev/peps/pep-0020)).\n", "* \u5982\u679c\u4f60\u8ba4\u4e3a\u8fd9\u5df2\u7ecf\u591f\u6709\u8da3\u7684\u4e86, \u53ef\u4ee5\u770b\u770b [this.py](https://hg.python.org/cpython/file/c3896275c0f6/Lib/this.py) \u7684\u5b9e\u73b0. \u6709\u8da3\u7684\u662f, Python \u4e4b\u7985\u7684\u5b9e\u73b0\u4ee3\u7801\u8fdd\u53cd\u4e86\u4ed6\u81ea\u5df1 (\u8fd9\u53ef\u80fd\u662f\u552f\u4e00\u4f1a\u53d1\u751f\u8fd9\u79cd\u60c5\u51b5\u7684\u5730\u65b9).\n", "*\n", "\u81f3\u4e8e `love is not True or False; love is love`, \u610f\u5916\u5374\u53c8\u4e0d\u8a00\u800c\u55bb.\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### > Yes, it exists!/\u662f\u7684, \u5b83\u5b58\u5728!\n", "**\u5faa\u73af\u7684 `else`.** \u4e00\u4e2a\u5178\u578b\u7684\u4f8b\u5b50:\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ " def does_exists_num(l, to_find):\n", " for num in l:\n", " if num == to_find:\n", " print(\"Exists!\")\n", " break\n", " else:\n", " print(\"Does not exist\")\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "**Output:**\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "Exists!\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "some_list = [1, 2, 3, 4, 5]\n", "does_exists_num(some_list, 4)\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "Does not exist\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "does_exists_num(some_list, -1)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "**\u5f02\u5e38\u7684 `else` .** \u4f8b,\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "try:\n", " pass\n", "except:\n", " print(\"Exception occurred!!!\")\n", "else:\n", " print(\"Try block executed successfully...\")\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "**Output:**\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "Try block executed successfully...\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### \ud83d\udca1 \u8bf4\u660e:\n", "- \u5faa\u73af\u540e\u7684 `else` \u5b50\u53e5\u53ea\u4f1a\u5728\u5faa\u73af\u6ca1\u6709\u89e6\u53d1 `break` \u8bed\u53e5, \u6b63\u5e38\u7ed3\u675f\u7684\u60c5\u51b5\u4e0b\u624d\u4f1a\u6267\u884c.\n", "- try \u4e4b\u540e\u7684 `else` \u5b50\u53e5\u4e5f\u88ab\u79f0\u4e3a \"\u5b8c\u6210\u5b50\u53e5\", \u56e0\u4e3a\u5728 `try` \u8bed\u53e5\u4e2d\u5230\u8fbe `else` \u5b50\u53e5\u610f\u5473\u7740try\u5757\u5b9e\u9645\u4e0a\u5df2\u6210\u529f\u5b8c\u6210.\n", "\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### > Ellipsis/\u7701\u7565 *\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "def some_func():\n", " Ellipsis\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "**Output**\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "# \u6ca1\u6709\u8f93\u51fa\uff0c\u4e5f\u6ca1\u6709\u62a5\u9519\n", "\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "some_func()\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "Traceback (most recent call last):\n", " File \"\", line 1, in \n", "NameError: name 'SomeRandomString' is not defined\n", "\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "SomeRandomString\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "Ellipsis\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "Ellipsis\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### \ud83d\udca1 \u8bf4\u660e\n", "- \u5728 Python \u4e2d\uff0c`Ellipsis` \u662f\u4e00\u4e2a\u5168\u5c40\u53ef\u7528\u7684\u5185\u7f6e\u5bf9\u8c61\uff0c\u76f8\u5f53\u4e8e`...`\u3002\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ " Ellipsis\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ " ...\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "- \u7701\u7565\u53f7\u53ef\u7528\u4e8e\u591a\u79cd\u7528\u9014\uff0c\n", " + \u4f5c\u4e3a\u5c1a\u672a\u7f16\u5199\u7684\u4ee3\u7801\u7684\u5360\u4f4d\u7b26\uff08\u5c31\u50cf`pass`\u8bed\u53e5\uff09\n", " + \u5728\u5207\u7247\u8bed\u6cd5\u4e2d\u8868\u793a\u5b8c\u6574\u5207\u7247\u7684\u5176\u4f59\u7ef4\u5ea6\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ " array([\n", " [\n", " [0, 1],\n", " [2, 3]\n", " ],\n", "\n", " [\n", " [4, 5],\n", " [6, 7]\n", " ]\n", " ])\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ " import numpy as np\n", " three_dimensional_array = np.arange(8).reshape(2, 2, 2)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", " \u6240\u4ee5\u6211\u4eec\u7684 `three_dimensional_array` \u662f\u4e00\u4e2a\u6570\u7ec4\u7684\u6570\u7ec4\u7684\u6570\u7ec4\u3002\u5047\u8bbe\u6211\u4eec\u8981\u6253\u5370\u6240\u6709\u6700\u5185\u5c42\u6570\u7ec4\u7684\u7b2c\u4e8c\u4e2a\u5143\u7d20\uff08\u7d22\u5f15 `1`\uff09\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 Ellipsis \u7ed5\u8fc7\u6240\u6709\u524d\u9762\u7684\u7ef4\u5ea6\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ " array([[1, 3],\n", " [5, 7]])\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ " three_dimensional_array[:,:,1]\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ " array([[1, 3],\n", " [5, 7]])\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ " three_dimensional_array[..., 1] # \u4f7f\u7528Ellipsis.\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", " \u6ce8\u610f\uff1a\u8fd9\u9002\u7528\u4e8e\u4efb\u610f\u6570\u91cf\u7684\u7ef4\u5ea6\u3002\u60a8\u751a\u81f3\u53ef\u4ee5\u5728\u7b2c\u4e00\u4e2a\u548c\u6700\u540e\u4e00\u4e2a\u7ef4\u5ea6\u4e2d\u9009\u62e9\u5207\u7247\u5e76\u4ee5\u8fd9\u79cd\u65b9\u5f0f\u5ffd\u7565\u4e2d\u95f4\u7684\u5207\u7247(`n_dimensional_array[firs_dim_slice, ..., last_dim_slice]`)\n", "\n", " + \u5728 [\u7c7b\u578b\u63d0\u793a](https://docs.python.org/3/library/typing.html)\u4e2d\u4ec5\u8868\u793a\u7c7b\u578b\u7684\u4e00\u90e8\u5206\uff08\u5982 `(Callable[..., int]` \u6216 `Tuple[ str, ...]`))\n", "\n", " + \u60a8\u4e5f\u53ef\u4ee5\u4f7f\u7528\u7701\u7565\u53f7\u4f5c\u4e3a\u9ed8\u8ba4\u51fd\u6570\u53c2\u6570\uff08\u5728\u60a8\u60f3\u8981\u533a\u5206\u201c\u65e0\u53c2\u6570\u201d\u548c\u201c\u4f20\u9012None\u503c\u201d\u573a\u666f\u7684\u60c5\u51b5\u4e0b\uff09\u3002\n", "\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### > Inpinity/\u65e0\u9650 *\n", "\u82f1\u6587\u62fc\u5199\u662f\u6709\u610f\u7684, \u8bf7\u4e0d\u8981\u4e3a\u6b64\u63d0\u4ea4\u8865\u4e01.\n", "(\u8bd1: \u8fd9\u91cc\u662f\u4e3a\u4e86\u7a81\u51fa Python \u4e2d\u65e0\u9650\u7684\u5b9a\u4e49\u4e0e[Pi](https://en.wikipedia.org/wiki/Pi)\u6709\u5173, \u6240\u4ee5\u5c06\u4e24\u4e2a\u5355\u8bcd\u62fc\u63a5\u4e86.)\n", "\n", "**Output (Python 3.x):**\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "314159\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "infinity = float('infinity')\n", "hash(infinity)\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "-314159\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "hash(float('-inf'))\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### \ud83d\udca1 \u8bf4\u660e:\n", "- infinity \u7684\u54c8\u5e0c\u503c\u662f 10\u2075 x \u03c0.\n", "- \u6709\u610f\u601d\u7684\u662f, `float('-inf')` \u7684\u54c8\u5e0c\u503c\u5728 Python 3 \u4e2d\u662f \"-10\u2075 x \u03c0\" , \u800c\u5728 Python 2 \u4e2d\u662f \"-10\u2075 x e\".\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### > Let's mangle/\u4fee\u9970\u65f6\u95f4! *\n", "1\\.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "class Yo(object):\n", " def __init__(self):\n", " self.__honey = True\n", " self.bro = True\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "**Output:**\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "True\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "Yo().bro\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "AttributeError: 'Yo' object has no attribute '__honey'\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "Yo().__honey\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "True\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "Yo()._Yo__honey\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "2\\.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "class Yo(object):\n", " def __init__(self):\n", " # \u8fd9\u6b21\u8bd5\u8bd5\u5bf9\u79f0\u5f62\u5f0f\n", " self.__honey__ = True\n", " self.bro = True\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "**Output:**\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "True\n", "\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "Yo().bro\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "Traceback (most recent call last):\n", " File \"\", line 1, in \n", "AttributeError: 'Yo' object has no attribute '_Yo__honey__'\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "Yo()._Yo__honey__\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "\u4e3a\u4ec0\u4e48 `Yo()._Yo__honey` \u80fd\u8fd0\u884c? \u53ea\u6709\u5370\u5ea6\u4eba\u7406\u89e3.(\u8bd1: \u8fd9\u4e2a\u6897\u53ef\u80fd\u662f\u6307\u5370\u5ea6\u97f3\u4e50\u4eba[Yo Yo Honey Singh](https://en.wikipedia.org/wiki/Yo_Yo_Honey_Singh))\n", "\n", "\n", "3\\.\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "_A__variable = \"Some value\"\n", "\n", "class A(object):\n", " def some_func(self):\n", " return __variable # \u6ca1\u5728\u4efb\u4f55\u5730\u65b9\u521d\u59cb\u5316\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "**Output:**\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "Traceback (most recent call last):\n", " File \"\", line 1, in \n", "AttributeError: 'A' object has no attribute '__variable'\n", "\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "A().__variable\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "'Some value'\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "A().some_func()\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### \ud83d\udca1 \u8bf4\u660e:\n", "\n", "* [\u547d\u540d\u4fee\u9970](https://en.wikipedia.org/wiki/Name_mangling) \u7528\u4e8e\u907f\u514d\u4e0d\u540c\u547d\u540d\u7a7a\u95f4\u4e4b\u95f4\u540d\u79f0\u51b2\u7a81.\n", "* \u5728 Python \u4e2d, \u89e3\u91ca\u5668\u4f1a\u901a\u8fc7\u7ed9\u7c7b\u4e2d\u4ee5 `__` (\u53cc\u4e0b\u5212\u7ebf)\u5f00\u5934\u4e14\u7ed3\u5c3e\u6700\u591a\u53ea\u6709\u4e00\u4e2a\u4e0b\u5212\u7ebf\u7684\u7c7b\u6210\u5458\u540d\u79f0\u52a0\u4e0a`_NameOfTheClass` \u6765\u4fee\u9970(mangles)\u540d\u79f0.\n", "* \u6240\u4ee5, \u8981\u8bbf\u95ee `__honey` \u5bf9\u8c61,\u6211\u4eec\u9700\u8981\u52a0\u4e0a `_Yo` \u4ee5\u9632\u6b62\u4e0e\u5176\u4ed6\u7c7b\u4e2d\u5b9a\u4e49\u7684\u76f8\u540c\u540d\u79f0\u7684\u5c5e\u6027\u53d1\u751f\u51b2\u7a81.\n", "* \u4f46\u662f\u4e3a\u4ec0\u4e48\u5b83\u5728\u7b2c\u4e8c\u4e2a\u7247\u6bb5\u4e2d\u4e0d\u8d77\u4f5c\u7528\uff1f \u56e0\u4e3a\u547d\u540d\u4fee\u9970\u6392\u9664\u4e86\u4ee5\u53cc\u4e0b\u5212\u7ebf\u7ed3\u5c3e\u7684\u540d\u79f0\u3002\n", "* \u7b2c\u4e09\u4e2a\u7247\u6bb5\u4e5f\u662f\u547d\u540d\u4fee\u9970\u7684\u7ed3\u679c\u3002 `return __variable` \u8bed\u53e5\u4e2d\u7684 `__variable` \u540d\u79f0\u88ab\u4fee\u6539\u4e3a `_A__variable`\uff0c\u8fd9\u4e5f\u6070\u597d\u662f\u6211\u4eec\u5728\u5916\u90e8\u4f5c\u7528\u57df\u4e2d\u58f0\u660e\u7684\u53d8\u91cf\u7684\u540d\u79f0\u3002\n", "* \u6b64\u5916\uff0c\u5982\u679c\u4fee\u9970\u540e\u7684\u53d8\u91cf\u540d\u8d85\u8fc7255\u4e2a\u5b57\u7b26\uff0c\u5219\u4f1a\u8fdb\u884c\u622a\u65ad\u3002\n", "\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### > Skipping lines?/\u8df3\u8fc7\u4e00\u884c?\n", "**Output:**\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "11\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "value = 11\n", "valu\u0435 = 32\n", "value\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "\u4ec0\u4e48\u9b3c?\n", "\n", "**\u6ce8\u610f:** \u5982\u679c\u4f60\u60f3\u8981\u91cd\u73b0\u7684\u8bdd\u6700\u7b80\u5355\u7684\u65b9\u6cd5\u662f\u76f4\u63a5\u590d\u5236\u4e0a\u9762\u7684\u4ee3\u7801\u7247\u6bb5\u5230\u4f60\u7684\u6587\u4ef6\u6216\u547d\u4ee4\u884c\u91cc.\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### \ud83d\udca1 \u8bf4\u660e:\n", "\n", "\u4e00\u4e9b\u975e\u897f\u65b9\u5b57\u7b26\u867d\u7136\u770b\u8d77\u6765\u548c\u82f1\u8bed\u5b57\u6bcd\u76f8\u540c, \u4f46\u4f1a\u88ab\u89e3\u91ca\u5668\u8bc6\u522b\u4e3a\u4e0d\u540c\u7684\u5b57\u6bcd.\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "1077\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "ord('\u0435') # \u897f\u91cc\u5c14\u8bed\u7684 'e' (Ye)\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "101\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "ord('e') # \u62c9\u4e01\u8bed\u7684 'e', \u7528\u4e8e\u82f1\u6587\u5e76\u4f7f\u7528\u6807\u51c6\u952e\u76d8\u8f93\u5165\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "False\n", "\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "'\u0435' == 'e'\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "42\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "value = 42 # \u62c9\u4e01\u8bed e\n", "valu\u0435 = 23 # \u897f\u91cc\u5c14\u8bed 'e', Python 2.x \u7684\u89e3\u91ca\u5668\u5728\u8fd9\u4f1a\u629b\u51fa `SyntaxError` \u5f02\u5e38\n", "value\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "\u5185\u7f6e\u7684 `ord()` \u51fd\u6570\u53ef\u4ee5\u8fd4\u56de\u4e00\u4e2a\u5b57\u7b26\u7684 Unicode [\u4ee3\u7801\u70b9](https://en.wikipedia.org/wiki/Code_point), \u8fd9\u91cc\u897f\u91cc\u5c14\u8bed 'e' \u548c\u62c9\u4e01\u8bed 'e' \u7684\u4ee3\u7801\u70b9\u4e0d\u540c\u8bc1\u5b9e\u4e86\u4e0a\u8ff0\u4f8b\u5b50.\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### > Teleportation/\u7a7a\u95f4\u79fb\u52a8 *\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "import numpy as np\n", "\n", "def energy_send(x):\n", " # \u521d\u59cb\u5316\u4e00\u4e2a numpy \u6570\u7ec4\n", " np.array([float(x)])\n", "\n", "def energy_receive():\n", " # \u8fd4\u56de\u4e00\u4e2a\u7a7a\u7684 numpy \u6570\u7ec4\n", " return np.empty((), dtype=np.float).tolist()\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "**Output:**\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "123.456\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "energy_send(123.456)\n", "energy_receive()\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "\u8c01\u6765\u7ed9\u6211\u53d1\u4e2a\u8bfa\u8d1d\u5c14\u5956?\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### \ud83d\udca1 \u8bf4\u660e:\n", "\n", "* \u6ce8\u610f\u5728 `energy_send` \u51fd\u6570\u4e2d\u521b\u5efa\u7684 numpy \u6570\u7ec4\u5e76\u6ca1\u6709\u8fd4\u56de, \u56e0\u6b64\u5185\u5b58\u7a7a\u95f4\u88ab\u91ca\u653e\u5e76\u53ef\u4ee5\u88ab\u91cd\u65b0\u5206\u914d.\n", "* `numpy.empty()` \u76f4\u63a5\u8fd4\u56de\u4e0b\u4e00\u6bb5\u7a7a\u95f2\u5185\u5b58\uff0c\u800c\u4e0d\u91cd\u65b0\u521d\u59cb\u5316. \u800c\u8fd9\u4e2a\u5185\u5b58\u70b9\u6070\u597d\u5c31\u662f\u521a\u521a\u91ca\u653e\u7684\u90a3\u4e2a(\u901a\u5e38\u60c5\u51b5\u4e0b, \u5e76\u4e0d\u7edd\u5bf9).\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### > Well, something is fishy.../\u55ef\uff0c\u6709\u4e9b\u53ef\u7591...\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "def square(x):\n", " \"\"\"\n", " \u4e00\u4e2a\u901a\u8fc7\u52a0\u6cd5\u8ba1\u7b97\u5e73\u65b9\u7684\u7b80\u5355\u51fd\u6570.\n", " \"\"\"\n", " sum_so_far = 0\n", " for counter in range(x):\n", " sum_so_far = sum_so_far + x\n", " return sum_so_far\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "**Output (Python 2.x):**\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "10\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "square(10)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "\u96be\u9053\u4e0d\u5e94\u8be5\u662f100\u5417?\n", "\n", "**\u6ce8\u610f:** \u5982\u679c\u4f60\u65e0\u6cd5\u91cd\u73b0, \u53ef\u4ee5\u5c1d\u8bd5\u8fd0\u884c\u8fd9\u4e2a\u6587\u4ef6[mixed_tabs_and_spaces.py](/mixed_tabs_and_spaces.py).\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### \ud83d\udca1 \u8bf4\u660e:\n", "\n", "* **\u4e0d\u8981\u6df7\u7528\u5236\u8868\u7b26(tab)\u548c\u7a7a\u683c(space)!** \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d, return \u7684\u524d\u9762\u662f\"1\u4e2a\u5236\u8868\u7b26\", \u800c\u5176\u4ed6\u90e8\u5206\u7684\u4ee3\u7801\u524d\u9762\u662f \"4\u4e2a\u7a7a\u683c\".\n", "* Python\u662f\u8fd9\u4e48\u5904\u7406\u5236\u8868\u7b26\u7684:\n", " > \u9996\u5148, \u5236\u8868\u7b26\u4f1a\u4ece\u5de6\u5230\u53f3\u4f9d\u6b21\u88ab\u66ff\u6362\u62108\u4e2a\u7a7a\u683c, \u76f4\u5230\u88ab\u66ff\u6362\u540e\u7684\u5b57\u7b26\u603b\u6570\u662f\u516b\u7684\u500d\u6570 <...>\n", "* \u56e0\u6b64, `square` \u51fd\u6570\u6700\u540e\u4e00\u884c\u7684\u5236\u8868\u7b26\u4f1a\u88ab\u66ff\u6362\u62108\u4e2a\u7a7a\u683c, \u5bfc\u81f4return\u8bed\u53e5\u8fdb\u5165\u5faa\u73af\u8bed\u53e5\u91cc\u9762.\n", "* Python 3 \u5f88\u53cb\u597d, \u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\u4f1a\u81ea\u52a8\u629b\u51fa\u9519\u8bef.\n", "\n", " **Output (Python 3.x):**\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ " TabError: inconsistent use of tabs and spaces in indentation\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### > `+=` is faster/\u66f4\u5feb\u7684 `+=`\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "0.25748300552368164\n", "# \u7528 \"+=\" \u8fde\u63a5\u4e09\u4e2a\u5b57\u7b26\u4e32:\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "# \u7528 \"+\" \u8fde\u63a5\u4e09\u4e2a\u5b57\u7b26\u4e32:\n", "timeit.timeit(\"s1 = s1 + s2 + s3\", setup=\"s1 = ' ' * 100000; s2 = ' ' * 100000; s3 = ' ' * 100000\", number=100)\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "0.012188911437988281\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "timeit.timeit(\"s1 += s2 + s3\", setup=\"s1 = ' ' * 100000; s2 = ' ' * 100000; s3 = ' ' * 100000\", number=100)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### \ud83d\udca1 \u8bf4\u660e:\n", "+ \u8fde\u63a5\u4e24\u4e2a\u4ee5\u4e0a\u7684\u5b57\u7b26\u4e32\u65f6 `+=` \u6bd4 `+` \u66f4\u5feb, \u56e0\u4e3a\u5728\u8ba1\u7b97\u8fc7\u7a0b\u4e2d\u7b2c\u4e00\u4e2a\u5b57\u7b26\u4e32 (\u4f8b\u5982, `s1 += s2 + s3` \u4e2d\u7684 `s1`) \u4e0d\u4f1a\u88ab\u9500\u6bc1.(\u8bd1: \u5c31\u662f `+=` \u6267\u884c\u7684\u662f\u8ffd\u52a0\u64cd\u4f5c\uff0c\u5c11\u4e86\u4e00\u4e2a\u9500\u6bc1\u65b0\u5efa\u7684\u52a8\u4f5c.)\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### > Let's make a giant string!/\u6765\u505a\u4e2a\u5de8\u5927\u7684\u5b57\u7b26\u4e32\u5427\uff01\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "def add_string_with_plus(iters):\n", " s = \"\"\n", " for i in range(iters):\n", " s += \"xyz\"\n", " assert len(s) == 3*iters\n", "\n", "def add_bytes_with_plus(iters):\n", " s = b\"\"\n", " for i in range(iters):\n", " s += b\"xyz\"\n", " assert len(s) == 3*iters\n", "\n", "def add_string_with_format(iters):\n", " fs = \"{}\"*iters\n", " s = fs.format(*([\"xyz\"]*iters))\n", " assert len(s) == 3*iters\n", "\n", "def add_string_with_join(iters):\n", " l = []\n", " for i in range(iters):\n", " l.append(\"xyz\")\n", " s = \"\".join(l)\n", " assert len(s) == 3*iters\n", "\n", "def convert_list_to_string(l, iters):\n", " s = \"\".join(l)\n", " assert len(s) == 3*iters\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "**Output:**\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "1000 loops, best of 3: 972 \u00b5s per loop\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "timeit(add_string_with_plus(10000))\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "1000 loops, best of 3: 815 \u00b5s per loop\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "timeit(add_bytes_with_plus(10000))\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "1000 loops, best of 3: 508 \u00b5s per loop\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "timeit(add_string_with_format(10000))\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "1000 loops, best of 3: 878 \u00b5s per loop\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "timeit(add_string_with_join(10000))\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "10000 loops, best of 3: 80 \u00b5s per loop\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "l = [\"xyz\"]*10000\n", "timeit(convert_list_to_string(l, 10000))\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "\u8ba9\u6211\u4eec\u5c06\u8fed\u4ee3\u6b21\u6570\u589e\u52a010\u500d.\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "100 loops, best of 3: 9.75 ms per loop\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "timeit(add_string_with_plus(100000)) # \u6267\u884c\u65f6\u95f4\u7ebf\u6027\u589e\u52a0\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "1000 loops, best of 3: 974 ms per loop\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "timeit(add_bytes_with_plus(100000)) # \u4e8c\u6b21\u589e\u52a0\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "100 loops, best of 3: 5.25 ms per loop\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "timeit(add_string_with_format(100000)) # \u7ebf\u6027\u589e\u52a0\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "100 loops, best of 3: 9.85 ms per loop\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "timeit(add_string_with_join(100000)) # \u7ebf\u6027\u589e\u52a0\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "1000 loops, best of 3: 723 \u00b5s per loop\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "l = [\"xyz\"]*100000\n", "timeit(convert_list_to_string(l, 100000)) # \u7ebf\u6027\u589e\u52a0\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### \ud83d\udca1 \u8bf4\u660e:\n", "- \u4f60\u53ef\u4ee5\u5728\u8fd9\u83b7\u5f97\u66f4\u591a [timeit](https://docs.python.org/3/library/timeit.html) \u7684\u76f8\u5173\u4fe1\u606f. \u5b83\u901a\u5e38\u7528\u4e8e\u8861\u91cf\u4ee3\u7801\u7247\u6bb5\u7684\u6267\u884c\u65f6\u95f4.\n", "- \u4e0d\u8981\u7528 `+` \u53bb\u751f\u6210\u8fc7\u957f\u7684\u5b57\u7b26\u4e32, \u5728 Python \u4e2d, `str` \u662f\u4e0d\u53ef\u53d8\u7684, \u6240\u4ee5\u5728\u6bcf\u6b21\u8fde\u63a5\u4e2d\u4f60\u90fd\u8981\u628a\u5de6\u53f3\u4e24\u4e2a\u5b57\u7b26\u4e32\u590d\u5236\u5230\u65b0\u7684\u5b57\u7b26\u4e32\u4e2d. \u5982\u679c\u4f60\u8fde\u63a5\u56db\u4e2a\u957f\u5ea6\u4e3a10\u7684\u5b57\u7b26\u4e32, \u4f60\u9700\u8981\u62f7\u8d1d (10+10) + ((10+10)+10) + (((10+10)+10)+10) = 90 \u4e2a\u5b57\u7b26\u800c\u4e0d\u662f 40 \u4e2a\u5b57\u7b26. \u968f\u7740\u5b57\u7b26\u4e32\u7684\u6570\u91cf\u548c\u5927\u5c0f\u7684\u589e\u52a0, \u60c5\u51b5\u4f1a\u53d8\u5f97\u8d8a\u53d1\u7684\u7cdf\u7cd5 (\u5c31\u50cf`add_bytes_with_plus` \u51fd\u6570\u7684\u6267\u884c\u65f6\u95f4\u4e00\u6837)\n", "- \u56e0\u6b64, \u66f4\u5efa\u8bae\u4f7f\u7528 `.format.` \u6216 `%` \u8bed\u6cd5 (\u4f46\u662f, \u5bf9\u4e8e\u77ed\u5b57\u7b26\u4e32, \u5b83\u4eec\u6bd4 `+` \u7a0d\u6162\u4e00\u70b9).\n", "- \u53c8\u6216\u8005, \u5982\u679c\u4f60\u6240\u9700\u7684\u5185\u5bb9\u5df2\u7ecf\u4ee5\u53ef\u8fed\u4ee3\u5bf9\u8c61\u7684\u5f62\u5f0f\u63d0\u4f9b\u4e86, \u4f7f\u7528 `''.join(\u53ef\u8fed\u4ee3\u5bf9\u8c61)` \u8981\u5feb\u591a\u4e86.\n", "- `add_string_with_plus` \u7684\u6267\u884c\u65f6\u95f4\u6ca1\u6709\u50cf `add_bytes_with_plus` \u4e00\u6837\u51fa\u73b0\u4e8c\u6b21\u589e\u52a0\u662f\u56e0\u4e3a\u89e3\u91ca\u5668\u4f1a\u5982\u540c\u4e0a\u4e00\u4e2a\u4f8b\u5b50\u6240\u8ba8\u8bba\u7684\u4e00\u6837\u4f18\u5316 `+=`. \u7528 `s = s + \"x\" + \"y\" + \"z\"` \u66ff\u4ee3 `s += \"xyz\"` \u7684\u8bdd, \u6267\u884c\u65f6\u95f4\u5c31\u4f1a\u4e8c\u6b21\u589e\u52a0\u4e86.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ " 100 loops, best of 3: 9.87 ms per loop\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ " def add_string_with_plus(iters):\n", " s = \"\"\n", " for i in range(iters):\n", " s = s + \"x\" + \"y\" + \"z\"\n", " assert len(s) == 3*iters\n", "\n", " timeit(add_string_with_plus(10000))\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ " 1 loops, best of 3: 1.09 s per loop\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ " timeit(add_string_with_plus(100000)) # \u6267\u884c\u65f6\u95f4\u4e8c\u6b21\u589e\u52a0\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### > Slowing down `dict` lookups/\u8ba9\u5b57\u5178\u7684\u67e5\u627e\u6162\u4e0b\u6765 *\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "some_dict = {str(i): 1 for i in range(1_000_000)}\n", "another_dict = {str(i): 1 for i in range(1_000_000)}\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "**Output:**\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "28.6 ns \u00b1 0.115 ns per loop (mean \u00b1 std. dev. of 7 runs, 10000000 loops each)\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "%timeit some_dict['5']\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "37.2 ns \u00b1 0.265 ns per loop (mean \u00b1 std. dev. of 7 runs, 10000000 loops each)\n", "\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "some_dict[1] = 1\n", "%timeit some_dict['5']\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "28.5 ns \u00b1 0.142 ns per loop (mean \u00b1 std. dev. of 7 runs, 10000000 loops each)\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "%timeit another_dict['5']\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "Traceback (most recent call last):\n", " File \"\", line 1, in \n", "KeyError: 1\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "another_dict[1] # Trying to access a key that doesn't exist\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "38.5 ns \u00b1 0.0913 ns per loop (mean \u00b1 std. dev. of 7 runs, 10000000 loops each)\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "%timeit another_dict['5']\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "\u4e3a\u4ec0\u4e48\u76f8\u540c\u7684\u67e5\u627e\u4f1a\u53d8\u5f97\u8d8a\u6765\u8d8a\u6162\uff1f\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### \ud83d\udca1 \u8bf4\u660e\n", "+ CPython \u6709\u4e00\u4e2a\u901a\u7528\u7684\u5b57\u5178\u67e5\u627e\u51fd\u6570\uff0c\u53ef\u4ee5\u5904\u7406\u6240\u6709\u7c7b\u578b\u7684\u952e\uff08`str`\u3001`int`\u3001\u4efb\u4f55\u5bf9\u8c61...\uff09\uff0c\u4ee5\u53ca\u4e00\u4e2a\u4e13\u95e8\u7528\u4e8e\u5904\u7406\u4ec5\u7531 `str` \u952e\u7ec4\u6210\u7684\u5b57\u5178\u7684\u5e38\u89c1\u60c5\u51b5\u3002\n", "+ \u4e13\u7528\u51fd\u6570\uff08\u5728 CPython \u7684 [\u6e90](https://github.com/python/cpython/blob/522691c46e2ae51faaad5bbbce7d959dd61770df/Objects/dictobject.c#L841) \u4e2d\u540d\u4e3a `lookdict_unicode`\uff09\u77e5\u9053\u6240\u6709\u73b0\u6709\u7684\u952e\uff08\u5305\u62ec\u67e5\u627e\u7684 key) \u662f\u5b57\u7b26\u4e32\uff0c\u5e76\u4f7f\u7528\u66f4\u5feb\u548c\u66f4\u7b80\u5355\u7684\u5b57\u7b26\u4e32\u6bd4\u8f83\u6765\u6bd4\u8f83\u952e\uff0c\u800c\u4e0d\u662f\u8c03\u7528 `__eq__` \u65b9\u6cd5\u3002\n", "+ \u7b2c\u4e00\u6b21\u4f7f\u7528\u975e `str` \u952e\u8bbf\u95ee `dict` \u5b9e\u4f8b\u65f6\uff0c\u4f1a\u5bf9\u5176\u8fdb\u884c\u4fee\u6539\uff0c\u4ee5\u4fbf\u5c06\u6765\u7684\u67e5\u627e\u4f7f\u7528\u901a\u7528\u51fd\u6570\u3002\n", "+ \u8fd9\u4e2a\u8fc7\u7a0b\u5bf9\u4e8e\u7279\u5b9a\u7684 `dict` \u5b9e\u4f8b\u662f\u4e0d\u53ef\u9006\u7684\uff0c\u5e76\u4e14\u952e\u751a\u81f3\u4e0d\u5fc5\u5b58\u5728\u4e8e\u5b57\u5178\u4e2d\u3002 \u8fd9\u5c31\u662f\u4e3a\u4ec0\u4e48\u5bf9\u4e0d\u5b58\u5728\u7684\u952e\u8fdb\u884c\u67e5\u627e\u5177\u6709\u76f8\u540c\u526f\u4f5c\u7528\u7684\u539f\u56e0\u3002\n", "\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### > Bloating instance `dict`s/\u53d8\u81c3\u80bf\u7684`dict`\u5b9e\u4f8b\u4eec *\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "import sys\n", "\n", "class SomeClass:\n", " def __init__(self):\n", " self.some_attr1 = 1\n", " self.some_attr2 = 2\n", " self.some_attr3 = 3\n", " self.some_attr4 = 4\n", "\n", "\n", "def dict_size(o):\n", " return sys.getsizeof(o.__dict__)\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "**Output:** (Python 3.8, \u5176\u4ed6 Python 3 \u7684\u7248\u672c\u4e5f\u8bb8\u7a0d\u6709\u4e0d\u540c)\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "104\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "o1 = SomeClass()\n", "o2 = SomeClass()\n", "dict_size(o1)\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "104\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "dict_size(o2)\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "232\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "del o1.some_attr1\n", "o3 = SomeClass()\n", "dict_size(o3)\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "232\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "dict_size(o1)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "\u8ba9\u6211\u4eec\u5728\u4e00\u4e2a\u65b0\u7684\u89e3\u91ca\u5668\u4e2d\u518d\u8bd5\u4e00\u6b21\uff1a\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "104 # \u610f\u6599\u4e4b\u4e2d\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "o1 = SomeClass()\n", "o2 = SomeClass()\n", "dict_size(o1)\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "360\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "o1.some_attr5 = 5\n", "o1.some_attr6 = 6\n", "dict_size(o1)\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "272\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "dict_size(o2)\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "232\n" ] }, "output_type": "execute_result", "metadata": {}, "execution_count": null } ], "source": [ "o3 = SomeClass()\n", "dict_size(o3)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "\u662f\u4ec0\u4e48\u8ba9\u90a3\u4e9b\u5b57\u5178\u53d8\u5f97\u81c3\u80bf\uff1f \u4e3a\u4ec0\u4e48\u65b0\u521b\u5efa\u7684\u5bf9\u8c61\u4e5f\u4f1a\u53d8\u81c3\u80bf\uff1f\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### \ud83d\udca1 \u8bf4\u660e\n", "+ CPython \u80fd\u591f\u5728\u591a\u4e2a\u5b57\u5178\u4e2d\u91cd\u7528\u76f8\u540c\u7684\u201c\u952e\u201d\u5bf9\u8c61\u3002 \u8fd9\u6dfb\u52a0\u5728 [PEP 412](https://www.python.org/dev/peps/pep-0412/) \u4e2d\uff0c\u76ee\u7684\u662f\u51cf\u5c11\u5185\u5b58\u4f7f\u7528\uff0c\u7279\u522b\u662f\u5728\u5b9e\u4f8b\u5b57\u5178\u4e2d \u2014\u2014 \u952e\uff08\u5b9e\u4f8b\u5c5e\u6027\uff09\u51e0\u4e4e\u5728\u6240\u6709\u5b9e\u4f8b\u90fd\u901a\u7528\u3002\n", "+ \u8fd9\u79cd\u4f18\u5316\u5bf9\u4e8e\u5b9e\u4f8b\u5b57\u5178\u6765\u8bf4\u662f\u5341\u5206\u81ea\u7136\u7684\uff0c\u4f46\u5982\u679c\u67d0\u4e9b\u5047\u8bbe\u88ab\u6253\u7834\uff0c\u5b83\u5c31\u4f1a\u88ab\u7981\u7528\u3002\n", "+ \u5bc6\u94a5\u5171\u4eab\u5b57\u5178\u4e0d\u652f\u6301\u5220\u9664\uff1b\u5982\u679c\u5220\u9664\u4e86\u5b9e\u4f8b\u5c5e\u6027\uff0c\u5219\u5b57\u5178\u662f\u201c\u672a\u5171\u4eab\u7684\u201d\uff0c\u5e76\u4e14\u540c\u4e00\u7c7b\u7684\u6240\u6709\u672a\u6765\u5b9e\u4f8b\u90fd\u7981\u7528\u5bc6\u94a5\u5171\u4eab\u3002\n", "+ \u53e6\u5916\uff0c\u5982\u679c\u5b57\u5178\u952e\u5df2\u88ab\u8c03\u6574\u5927\u5c0f\uff08\u56e0\u4e3a\u63d2\u5165\u4e86\u65b0\u952e\uff09\uff0c\u5b83\u4eec\u4fdd\u6301\u5171\u4eab*\u4ec5\u5f53*\u5b83\u4eec\u88ab\u4e00\u4e2a\u5b8c\u5168\u5355\u4e00\u7684\u5b57\u5178\u4f7f\u7528\u65f6\uff08\u8fd9\u5141\u8bb8\u5728\u7b2c\u4e00\u4e2a\u521b\u5efa\u7684\u5b9e\u4f8b\u7684 `__init__` \u4e2d\u6dfb\u52a0\u8bb8\u591a\u5c5e\u6027\uff0c\u800c\u4e0d\u4f1a\u5bfc\u81f4\u201c\u53d6\u6d88\u5171\u4eab\u201d\uff09\u3002\u5982\u679c\u53d1\u751f\u8c03\u6574\u5927\u5c0f\u65f6\u5b58\u5728\u591a\u4e2a\u5b9e\u4f8b\uff0c\u5219\u4e3a\u540c\u4e00\u7c7b\u7684\u6240\u6709\u672a\u6765\u5b9e\u4f8b\u7981\u7528\u5bc6\u94a5\u5171\u4eab\uff1aCPython \u65e0\u6cd5\u5224\u65ad\u60a8\u7684\u5b9e\u4f8b\u662f\u5426\u6b63\u5728\u4f7f\u7528\u76f8\u540c\u7684\u5c5e\u6027\u96c6\uff0c\u5e76\u51b3\u5b9a\u653e\u5f03\u5c1d\u8bd5\u5171\u4eab\u5b83\u4eec\u7684\u952e\u503c\u3002\n", "+ \u4e00\u4e2a\u5c0f\u63d0\u793a\uff0c\u5982\u679c\u4f60\u7684\u76ee\u6807\u662f\u964d\u4f4e\u7a0b\u5e8f\u7684\u5185\u5b58\u5360\u7528\uff1a\u4e0d\u8981\u5220\u9664\u5b9e\u4f8b\u5c5e\u6027\uff0c\u5e76\u786e\u4fdd\u5728 `__init__` \u4e2d\u521d\u59cb\u5316\u6240\u6709\u7684\u5c5e\u6027\uff01\n", "\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### > Minor Ones/\u5c0f\u77e5\u8bc6\u70b9\n", "* `join()` \u662f\u4e00\u4e2a\u5b57\u7b26\u4e32\u64cd\u4f5c\u800c\u4e0d\u662f\u5217\u8868\u64cd\u4f5c. (\u7b2c\u4e00\u6b21\u63a5\u89e6\u4f1a\u89c9\u5f97\u6709\u70b9\u8fdd\u53cd\u76f4\u89c9)\n", "\n", " **\ud83d\udca1 \u8bf4\u660e:**\n", " \u5982\u679c `join()` \u662f\u5b57\u7b26\u4e32\u65b9\u6cd5 \u90a3\u4e48\u5b83\u5c31\u53ef\u4ee5\u5904\u7406\u4efb\u4f55\u53ef\u8fed\u4ee3\u7684\u5bf9\u8c61(\u5217\u8868\uff0c\u5143\u7ec4\uff0c\u8fed\u4ee3\u5668). \u5982\u679c\u5b83\u662f\u5217\u8868\u65b9\u6cd5, \u5219\u5fc5\u987b\u5728\u6bcf\u79cd\u7c7b\u578b\u4e2d\u5355\u72ec\u5b9e\u73b0. \u53e6\u5916, \u5728 `list` \u5bf9\u8c61\u7684\u901a\u7528API\u4e2d\u5b9e\u73b0\u4e00\u4e2a\u4e13\u7528\u4e8e\u5b57\u7b26\u4e32\u7684\u65b9\u6cd5\u6ca1\u6709\u592a\u5927\u7684\u610f\u4e49.\n", "\n", "* \u770b\u7740\u5947\u602a\u4f46\u80fd\u6b63\u786e\u8fd0\u884c\u7684\u8bed\u53e5:\n", " + `[] = ()` \u8bed\u53e5\u5728\u8bed\u4e49\u4e0a\u662f\u6b63\u786e\u7684 (\u89e3\u5305\u4e00\u4e2a\u7a7a\u7684 `tuple` \u5e76\u8d4b\u503c\u7ed9 `list`)\n", " + `'a'[0][0][0][0][0]` \u5728\u8bed\u4e49\u4e0a\u4e5f\u662f\u6b63\u786e\u7684, \u56e0\u4e3a\u5728 Python \u4e2d\u5b57\u7b26\u4e32\u540c\u65f6\u4e5f\u662f[\u5e8f\u5217](https://docs.python.org/3/glossary.html#term-sequence)(\u53ef\u8fed\u4ee3\u5bf9\u8c61\u652f\u6301\u4f7f\u7528\u6574\u6570\u7d22\u5f15\u8bbf\u95ee\u5143\u7d20).\n", " + `3 --0-- 5 == 8` \u548c `--5 == 5` \u5728\u8bed\u4e49\u4e0a\u90fd\u662f\u6b63\u786e\u7684, \u4e14\u7ed3\u679c\u7b49\u4e8e `True`.(\u8bd1: 3\u51cf\u8d1f0\u7b49\u4e8e3\uff0c\u518d\u51cf\u8d1f5\u76f8\u5f53\u4e8e\u52a05\u7b49\u4e8e8\uff1b\u8d1f\u7684\u8d1f5\u7b49\u4e8e5.)\n", "\n", "* \u9274\u4e8e `a` \u662f\u4e00\u4e2a\u6570\u5b57, `++a` \u548c `--a` \u90fd\u662f\u6709\u6548\u7684 Python \u8bed\u53e5, \u4f46\u5176\u6548\u679c\u4e0e C, C++ \u6216 Java \u7b49\u4e0d\u4e00\u6837.\n", " ```py\n", " >>> a = 5\n", " >>> a\n", " 5\n", " >>> ++a\n", " 5\n", " >>> --a\n", " 5\n", " ```\n", "\n", " **\ud83d\udca1 \u8bf4\u660e:**\n", " + python \u91cc\u6ca1\u6709 `++` \u64cd\u4f5c\u7b26. \u8fd9\u5176\u5b9e\u662f\u4e24\u4e2a `+` \u64cd\u4f5c\u7b26.\n", " + `++a` \u88ab\u89e3\u6790\u4e3a `+(+a)` \u6700\u540e\u7b49\u4e8e `a`. `--a` \u540c\u7406.\n", " + \u8fd9\u4e2a StackOverflow [\u56de\u7b54](https://stackoverflow.com/questions/3654830/why-are-there-no-and-operators-in-python) \u8ba8\u8bba\u4e86\u4e3a\u4ec0\u4e48 Python \u4e2d\u7f3a\u5c11\u589e\u91cf\u548c\u51cf\u91cf\u8fd0\u7b97\u7b26.\n", "\n", "* Python \u4f7f\u7528 2\u4e2a\u5b57\u8282\u5b58\u50a8\u51fd\u6570\u4e2d\u7684\u672c\u5730\u53d8\u91cf. \u7406\u8bba\u4e0a, \u8fd9\u610f\u5473\u7740\u51fd\u6570\u4e2d\u53ea\u80fd\u5b9a\u4e4965536\u4e2a\u53d8\u91cf. \u4f46\u662f\uff0cPython \u5185\u7f6e\u4e86\u4e00\u4e2a\u65b9\u4fbf\u7684\u89e3\u51b3\u65b9\u6848\uff0c\u53ef\u7528\u4e8e\u5b58\u50a8\u8d85\u8fc72^16\u4e2a\u53d8\u91cf\u540d. \u4e0b\u9762\u7684\u4ee3\u7801\u6f14\u793a\u4e86\u5f53\u5b9a\u4e49\u4e86\u8d85\u8fc765536\u4e2a\u5c40\u90e8\u53d8\u91cf\u65f6\u5806\u6808\u4e2d\u53d1\u751f\u7684\u60c5\u51b5 (\u8b66\u544a: \u8fd9\u6bb5\u4ee3\u7801\u4f1a\u6253\u5370\u5927\u7ea62^18\u884c\u6587\u672c, \u8bf7\u505a\u597d\u51c6\u5907!):\n", " ```py\n", " import dis\n", " exec(\"\"\"\n", " def f():\n", " \"\"\" + \"\"\"\n", " \"\"\".join([\"X\"+str(x)+\"=\" + str(x) for x in range(65539)]))\n", "\n", " f()\n", "\n", " print(dis.dis(f))\n", " ```\n", "\n", "* \u4f60\u7684 *Python \u4ee3\u7801* \u5e76\u4e0d\u4f1a\u591a\u7ebf\u7a0b\u540c\u65f6\u8fd0\u884c (\u662f\u7684, \u4f60\u6ca1\u542c\u9519!). \u867d\u7136\u4f60\u89c9\u5f97\u4f1a\u4ea7\u751f\u591a\u4e2a\u7ebf\u7a0b\u5e76\u8ba9\u5b83\u4eec\u540c\u65f6\u6267\u884c\u4f60\u7684\u4ee3\u7801, \u4f46\u662f, \u7531\u4e8e [\u5168\u5c40\u89e3\u91ca\u9501](https://wiki.python.org/moin/GlobalInterpreterLock)\u7684\u5b58\u5728, \u4f60\u6240\u505a\u7684\u53ea\u662f\u8ba9\u4f60\u7684\u7ebf\u7a0b\u4f9d\u6b21\u5728\u540c\u4e00\u4e2a\u6838\u5fc3\u4e0a\u6267\u884c. Python \u591a\u7ebf\u7a0b\u9002\u7528\u4e8eIO\u5bc6\u96c6\u578b\u7684\u4efb\u52a1, \u4f46\u5982\u679c\u60f3\u8981\u5e76\u884c\u5904\u7406CPU\u5bc6\u96c6\u578b\u7684\u4efb\u52a1, \u4f60\u5e94\u8be5\u4f1a\u60f3\u4f7f\u7528 [multiprocessing](https://docs.python.org/2/library/multiprocessing.html) \u6a21\u5757.\n", "\n", "* \u5217\u8868\u5207\u7247\u8d85\u51fa\u7d22\u5f15\u8fb9\u754c\u800c\u4e0d\u5f15\u53d1\u4efb\u4f55\u9519\u8bef\n", " ```py\n", " >>> some_list = [1, 2, 3, 4, 5]\n", " >>> some_list[111:]\n", " []\n", " ```\n", "\n", "* `int('\u0661\u0662\u0663\u0664\u0665\u0666\u0667\u0668\u0669')` \u5728 Python 3 \u4e2d\u4f1a\u8fd4\u56de `123456789`. \u5728 Python \u4e2d, \u5341\u8fdb\u5236\u5b57\u7b26\u5305\u62ec\u6570\u5b57\u5b57\u7b26, \u4ee5\u53ca\u53ef\u7528\u4e8e\u5f62\u6210\u5341\u8fdb\u5236\u6570\u5b57\u7684\u6240\u6709\u5b57\u7b26, \u4f8b\u5982: U+0660, ARABIC-INDIC DIGIT ZERO. \u8fd9\u6709\u4e00\u4e2a\u5173\u4e8e\u6b64\u7684 [\u6709\u8da3\u6545\u4e8b](http://chris.improbable.org/2014/8/25/adventures-in-unicode-digits/).\n", "\n", "* `'abc'.count('') == 4`. \u8fd9\u6709\u4e00\u4e2a `count` \u65b9\u6cd5\u7684\u76f8\u8fd1\u5b9e\u73b0, \u80fd\u66f4\u597d\u7684\u8bf4\u660e\u95ee\u9898\n", " ```py\n", " def count(s, sub):\n", " result = 0\n", " for i in range(len(s) + 1 - len(sub)):\n", " result += (s[i:i + len(sub)] == sub)\n", " return result\n", " ```\n", " \u8fd9\u4e2a\u884c\u4e3a\u662f\u7531\u4e8e\u7a7a\u5b50\u4e32(`''`)\u4e0e\u539f\u59cb\u5b57\u7b26\u4e32\u4e2d\u957f\u5ea6\u4e3a0\u7684\u5207\u7247\u76f8\u5339\u914d\u5bfc\u81f4\u7684.\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### > First things first!/\u8981\u4e8b\u4f18\u5148 *\n", "\u4f17\u6240\u5468\u77e5\uff0cPython 3.8 \u63a8\u51fa\"\u6d77\u8c61\"\u8fd0\u7b97\u7b26 (`:=`) \u65b9\u4fbf\u6613\u7528\uff0c\u8ba9\u6211\u4eec\u4e00\u8d77\u770b\u770b\u3002\n", "\n", "1\\.\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```py\n", "# Python \u7248\u672c 3.8+\n", "\n", ">>> a = \"wtf_walrus\"\n", ">>> a\n", "```\n", "```py\n", "'wtf_walrus'\n", "\n", "```\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```py\n", ">>> a := \"wtf_walrus\"\n", "```\n", "```py\n", "File \"\", line 1\n", " a := \"wtf_walrus\"\n", " ^\n", "SyntaxError: invalid syntax\n", "\n", "```\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```py\n", ">>> (a := \"wtf_walrus\") # \u8be5\u8bed\u53e5\u6709\u6548\n", "```\n", "```py\n", "'wtf_walrus'\n", "```\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```py\n", ">>> a\n", "```\n", "```py\n", "'wtf_walrus'\n", "```\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "2 \\.\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```py\n", "# Python \u7248\u672c 3.8+\n", "\n", ">>> a = 6, 9\n", ">>> a\n", "```\n", "```py\n", "(6, 9)\n", "\n", "```\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```py\n", ">>> (a := 6, 9)\n", "```\n", "```py\n", "(6, 9)\n", "```\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```py\n", ">>> a\n", "```\n", "```py\n", "6\n", "\n", "```\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```py\n", ">>> a, b = 6, 9 # \u5178\u578b\u62c6\u5305\u64cd\u4f5c\n", ">>> a, b\n", "```\n", "```py\n", "(6, 9)\n", "```\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```py\n", ">>> (a, b = 16, 19) # \u51fa\u9519\u5566\n", "```\n", "```py\n", " File \"\", line 1\n", " (a, b = 16, 19)\n", " ^\n", "SyntaxError: invalid syntax\n", "\n", "```\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```py\n", ">>> (a, b := 16, 19) # \u8fd9\u91cc\u7684\u7ed3\u679c\u662f\u4e00\u4e2a\u5947\u602a\u7684\u4e09\u5143\u7ec4\n", "```\n", "```py\n", "(6, 16, 19)\n", "\n", "```\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```py\n", ">>> a # a\u503c\u4ecd\u7136\u6ca1\u53d8?\n", "```\n", "```py\n", "6\n", "\n", "```\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```py\n", ">>> b\n", "```\n", "```py\n", "16\n", "```\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### \ud83d\udca1 \u8bf4\u660e\n", "\n", "**\u201c\u6d77\u8c61\u201d\u8fd0\u7b97\u7b26\u7b80\u4ecb**\n", "\n", "\u6d77\u8c61\u8fd0\u7b97\u7b26 (`:=`) \u5728Python 3.8\u4e2d\u88ab\u5f15\u5165\uff0c\u7528\u6765\u5728\u8868\u8fbe\u5f0f\u4e2d\u4e3a\u53d8\u91cf\u8d4b\u503c\u3002\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```py\n", "def some_func():\n", " # \u5047\u8bbe\u8fd9\u513f\u6709\u4e00\u4e9b\u8017\u65f6\u7684\u8ba1\u7b97\n", " # time.sleep(1000)\n", " return 5\n", "\n", "# \u5f15\u5165\u201c\u6d77\u8c61\u201d\u8fd0\u7b97\u7b26\u524d\u7684\u4f8b\u5b50\n", "if some_func():\n", " print(some_func()) # \u7cdf\u7cd5\u7684\u6848\u4f8b\u2014\u2014\u51fd\u6570\u8fd0\u7b97\u4e86\u4e24\u6b21\n", "\n", "# \u6216\u8005\uff0c\u52a0\u4ee5\u6539\u8fdb\uff1a\n", "a = some_func()\n", "if a:\n", " print(a)\n", "\n", "# \u6709\u4e86\u201c\u6d77\u8c61\u201d\u8fd0\u7b97\u7b26\uff0c\u4f60\u53ef\u4ee5\u5199\u7684\u66f4\u7b80\u6d01\uff1a\n", "if a := some_func():\n", " print(a)\n", "```\n", "```py\n", "```\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "**\u8f93\u51fa (> Python 3.8):**\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```py\n", "5\n", "5\n", "5\n", "```\n", "```py\n", "```\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\u8fd9\u6837\u65e2\u51cf\u5c11\u4e86\u4e00\u884c\u4ee3\u7801\uff0c\u53c8\u907f\u514d\u4e86\u4e24\u6b21\u8c03\u7528 `some_func` \u51fd\u6570\u3002\n", "\n", "- \u5728\u9876\u5c42\u7684\u65e0\u62ec\u53f7\u8d4b\u503c\u64cd\u4f5c\uff08\u4f7f\u7528\u201c\u6d77\u8c61\u201d\u8fd0\u7b97\u7b26\uff09\u88ab\u9650\u5236\uff0c\u56e0\u6b64\u4f8b1\u4e2d\u7684 `a := \"wtf_walrus\"` \u51fa\u73b0\u4e86 `SyntaxError` \u3002\u7528\u62ec\u53f7\u62ec\u8d77\u6765\u3002\u5b83\u5c31\u80fd\u6b63\u5e38\u5de5\u4f5c\u4e86\u3002\n", "\n", "- \u4e00\u822c\u7684\uff0c\u5305\u542b `=` \u64cd\u4f5c\u7684\u8868\u8fbe\u5f0f\u662f\u4e0d\u80fd\u7528\u62ec\u53f7\u62ec\u8d77\u6765\u7684\uff0c\u56e0\u6b64 `(a, b = 6, 9)` \u4e2d\u51fa\u73b0\u4e86\u8bed\u6cd5\u9519\u8bef\u3002\n", "\n", "- \u201c\u6d77\u8c61\u201d\u8fd0\u7b97\u7b26\u7684\u8bed\u6cd5\u5f62\u5f0f\u4e3a\uff1a`NAME:= expr`\uff0c`NAME` \u662f\u4e00\u4e2a\u6709\u6548\u7684\u6807\u8bc6\u7b26\uff0c\u800c `expr` \u662f\u4e00\u4e2a\u6709\u6548\u7684\u8868\u8fbe\u5f0f\u3002 \u56e0\u6b64\uff0c\u8fd9\u610f\u5473\u7740\u5b83\u4e0d\u652f\u6301\u53ef\u8fed\u4ee3\u7684\u6253\u5305\u548c\u62c6\u5305\u3002\n", "\n", " - `(a := 6, 9)` \u7b49\u4ef7\u4e8e `((a := 6), 9)` \uff0c\u6700\u7ec8\u5f97\u5230 `(a, 9) ` (\u5176\u4e2d `a` \u7684\u503c\u4e3a6)\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```py\n", " >>> (a := 6, 9) == ((a := 6), 9)\n", "```\n", "```py\n", " True\n", "```\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```py\n", " >>> x = (a := 696, 9)\n", " >>> x\n", "```\n", "```py\n", " (696, 9)\n", "```\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```py\n", " >>> x[0] is a # \u4e24\u4e2a\u53d8\u91cf\u6307\u5411\u540c\u4e00\u5185\u5b58\u7a7a\u95f4\n", "```\n", "```py\n", " True\n", "```\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", " - \u7c7b\u4f3c\u7684\uff0c `(a, b := 16, 19)` \u7b49\u4ef7\u4e8e `(a, (b := 16), 19)` \uff0c\u53ea\u662f\u4e00\u4e2a\u4e09\u5143\u7ec4\u3002 \n", "\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### > Wild imports/\u901a\u914d\u7b26\u5bfc\u5165\u65b9\u5f0f *\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```py\n", "# File: module.py\n", "\n", "def some_weird_name_func_():\n", " print(\"works!\")\n", "\n", "def _another_weird_name_func():\n", " print(\"works!\")\n", "\n", "```\n", "```py\n", "```\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "**Output**\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```py\n", ">>> from module import *\n", ">>> some_weird_name_func_()\n", "```\n", "```py\n", "\"works!\"\n", "```\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```py\n", ">>> _another_weird_name_func()\n", "```\n", "```py\n", "Traceback (most recent call last):\n", " File \"\", line 1, in \n", "NameError: name '_another_weird_name_func' is not defined\n", "```\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### \ud83d\udca1 \u8bf4\u660e\n", "\n", "- \u901a\u5e38\u5efa\u8bae\u4e0d\u8981\u4f7f\u7528\u901a\u914d\u7b26\u5bfc\u5165\u3002\u7b2c\u4e00\u4e2a\u660e\u663e\u7684\u539f\u56e0\u662f\uff0c\u5728\u901a\u914d\u7b26\u5bfc\u5165\u4e2d\uff0c\u5e26\u6709\u524d\u5bfc\u4e0b\u5212\u7ebf\u7684\u540d\u79f0\u4e0d\u4f1a\u88ab\u5bfc\u5165\u3002\u8fd9\u53ef\u80fd\u4f1a\u5bfc\u81f4\u8fd0\u884c\u65f6\u51fa\u9519\u3002\n", "- \u5982\u679c\u6211\u4eec\u4f7f\u7528 `from ... import a, b, c` \u8bed\u6cd5\uff0c\u4e0a\u9762\u7684 `NameError` \u5c31\u4e0d\u4f1a\u53d1\u751f\u3002\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```py\n", " >>> from module import some_weird_name_func_, _another_weird_name_func\n", " >>> _another_weird_name_func()\n", "```\n", "```py\n", " works!\n", "```\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "- \u5982\u679c\u4f60\u771f\u7684\u60f3\u4f7f\u7528\u901a\u914d\u7b26\u5bfc\u5165\uff0c\u90a3\u4e48\u4f60\u5fc5\u987b\u5728\u4f60\u7684\u6a21\u5757\u4e2d\u5b9a\u4e49\u5217\u8868`__all__`\uff0c\u5b83\u5305\u542b\u4e00\u7cfb\u5217\u516c\u5171\u5bf9\u8c61\uff0c\u5f53\u6211\u4eec\u8fdb\u884c\u901a\u914d\u7b26\u5bfc\u5165\u65f6\uff0c\u5217\u8868\u4e2d\u7684\u8fd9\u4e9b\u5bf9\u8c61\u5c06\u88ab\u5bfc\u5165\u3002\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```py\n", " __all__ = ['_another_weird_name_func']\n", "\n", " def some_weird_name_func_():\n", " print(\"works!\")\n", "\n", " def _another_weird_name_func():\n", " print(\"works!\")\n", "```\n", "```py\n", "```\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " **Output**\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```py\n", " >>> _another_weird_name_func()\n", "```\n", "```py\n", " \"works!\"\n", "```\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```py\n", " >>> some_weird_name_func_()\n", "```\n", "```py\n", " Traceback (most recent call last):\n", " File \"\", line 1, in \n", " NameError: name 'some_weird_name_func_' is not defined\n", "```\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Contributing/\u8d21\u732e\n", "\n", "\u6b22\u8fce\u5404\u79cd\u8865\u4e01! \u8be6\u60c5\u8bf7\u770b[CONTRIBUTING.md](https://github.com/satwikkansal/wtfpython/blob/master/CONTRIBUTING.md).(\u8bd1: \u8fd9\u662f\u7ed9\u539f\u5e93\u63d0\u8d21\u732e\u7684\u8981\u6c42\u6a21\u7248)\n", "\n", "\u4f60\u53ef\u4ee5\u901a\u8fc7\u65b0\u5efa [issue](https://github.com/satwikkansal/wtfpython/issues/new) \u6216\u8005\u5728\u4e0a [Gitter](https://gitter.im/wtfpython/Lobby) \u4e0e\u6211\u4eec\u8fdb\u884c\u8ba8\u8bba.\n", "\n", "(\u8bd1: \u5982\u679c\u4f60\u60f3\u5bf9\u8fd9\u4e2a\u7ffb\u8bd1\u9879\u76ee\u63d0\u4f9b\u5e2e\u52a9, \u8bf7\u770b[\u8fd9\u91cc](https://github.com/leisurelicht/wtfpython-cn/blob/master/CONTRIBUTING.md))\n", "\n", "# Acknowledgements/\u81f4\u8c22\n", "\n", "\u8fd9\u4e2a\u7cfb\u5217\u6700\u521d\u7684\u60f3\u6cd5\u548c\u8bbe\u8ba1\u7075\u611f\u6765\u81ea\u4e8e Denys Dovhan \u7684\u9879\u76ee [wtfjs](https://github.com/denysdovhan/wtfjs). \u793e\u533a\u7684\u5f3a\u5927\u652f\u6301\u8ba9\u5b83\u6210\u957f\u4e3a\u73b0\u5728\u7684\u6a21\u6837.\n", "\n", "#### Some nice Links!/\u4e00\u4e9b\u4e0d\u9519\u7684\u8d44\u6e90\n", "* https://www.youtube.com/watch?v=sH4XF6pKKmk\n", "* https://www.reddit.com/r/Python/comments/3cu6ej/what_are_some_wtf_things_about_python\n", "* https://sopython.com/wiki/Common_Gotchas_In_Python\n", "* https://stackoverflow.com/questions/530530/python-2-x-gotchas-and-landmines\n", "* https://stackoverflow.com/questions/1011431/common-pitfalls-in-python\n", "* https://www.python.org/doc/humor/\n", "* https://www.codementor.io/satwikkansal/python-practices-for-efficient-code-performance-memory-and-usability-aze6oiq65\n", "\n", "# \ud83c\udf93 License/\u8bb8\u53ef\n", "\n", "[![CC 4.0][license-image]][license-url]\n", "\n", "© [Satwik Kansal](https://satwikkansal.xyz)\n", "\n", "[license-url]: http://www.wtfpl.net\n", "[license-image]: https://img.shields.io/badge/License-WTFPL%202.0-lightgrey.svg?style=flat-square\n", "\n" ] } ], "metadata": {}, "nbformat": 4, "nbformat_minor": 2 }