{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "
\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 \"