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