一些有趣且鲜为人知的Python特性

新知榜官方账号

2023-10-08 02:29:08

一些有趣且鲜为人知的Python特性

Python,是一个设计优美的解释型高级语言,它提供了很多能让程序员感到舒适的功能特性。但有的时候,Python的一些输出结果对于初学者来说似乎并不是那么一目了然。这个有趣的项目意在收集Python中那些难以理解和反人类直觉的例子以及鲜为人知的功能特性,并尝试讨论这些现象背后真正的原理!虽然下面的有些例子并不一定会让你觉得WTFs,但它们依然有可能会告诉你一些你所不知道的Python有趣特性。我觉得这是一种学习编程语言内部原理的好办法,而且我相信你也会从中获得乐趣!如果您是一位经验比较丰富的Python程序员,你可以尝试挑战看是否能一次就找到例子的正确答案。你可能对其中的一些例子已经比较熟悉了,那这也许能唤起你当年踩这些坑时的甜蜜回忆。如果你不是第一次读了,你可以在这里获取变动内容。那么,让我们开始吧...

Table of Contents/目录

Table of Contents/目录

Structure of the Examples/示例结构

Structure of the Examples/示例结构

Usage/用法

Usage/用法

Examples/示例

Examples/示例

Section: Strain your brain!/大脑运动!

Section: Strain your brain!/大脑运动!

Strings can be tricky sometimes/微妙的字符串

Strings can be tricky sometimes/微妙的字符串

Time for some hash brownies!/是时候来点蛋糕了!

Time for some hash brownies!/是时候来点蛋糕了!

Return return everywhere!/到处返回!

Return return everywhere!/到处返回!

Deep down, we're all the same./本质上,我们都一样。

Deep down, we're all the same./本质上,我们都一样。

For what?/为什么?

For what?/为什么?

Evaluation time discrepancy/执行时机差异

Evaluation time discrepancy/执行时机差异

is not what it is!/出人意料的is!

is not what it is!/出人意料的is!

A tic-tac-toe where X wins in the first attempt!/一蹴即就!

A tic-tac-toe where X wins in the first attempt!/一蹴即就!

The sticky output function/麻烦的输出

The sticky output function/麻烦的输出

is not... is not is(not...)/is not...不是is(not...)

is not... is not is(not...)/is not...不是is(not...)

The surprising comma/意外的逗号

The surprising comma/意外的逗号

Backslashes at the end of string/字符串末尾的反斜杠

Backslashes at the end of string/字符串末尾的反斜杠

not knot!/别纠结!

not knot!/别纠结!

Half triple-quoted strings/三个引号

Half triple-quoted strings/三个引号

Midnight time doesn't exist?/不存在的午夜?

Midnight time doesn't exist?/不存在的午夜?

What's wrong with booleans?/布尔你咋了?

What's wrong with booleans?/布尔你咋了?

Class attributes and instance attributes/类属性和实例属性

Class attributes and instance attributes/类属性和实例属性

yielding None/生成None

yielding None/生成None

Mutating the immutable!/强人所难

Mutating the immutable!/强人所难

The disappearing variable from outer scope/消失的外部变量

The disappearing variable from outer scope/消失的外部变量

When True is actually False/真亦假

When True is actually False/真亦假

From filled to None in one instruction.../从有到无...

From filled to None in one instruction.../从有到无...

Subclass relationships/子类关系

Subclass relationships/子类关系

The mysterious key type conversion/神秘的键型转换

The mysterious key type conversion/神秘的键型转换

Let's see if you can guess this?/看看你能否猜到这一点?

Let's see if you can guess this?/看看你能否猜到这一点?

Section: Appearances are deceptive!/外表是靠不住的!

Section: Appearances are deceptive!/外表是靠不住的!

Skipping lines?/跳过一行?

Skipping lines?/跳过一行?

Teleportation/空间移动

Teleportation/空间移动

Well, something is fishy.../嗯,有些可疑...

Well, something is fishy.../嗯,有些可疑...

Section: Watch out for the landmines!/小心地雷!

Section: Watch out for the landmines!/小心地雷!

Modifying a dictionary while iterating over it/迭代字典时的修改

Modifying a dictionary while iterating over it/迭代字典时的修改

Stubborn del operator/坚强的del

Stubborn del operator/坚强的del

Deleting a list item while iterating/迭代列表时删除元素

Deleting a list item while iterating/迭代列表时删除元素

Loop variables leaking out!/循环变量泄漏!

Loop variables leaking out!/循环变量泄漏!

Beware of default mutable arguments!/当心默认的可变参数!

Beware of default mutable arguments!/当心默认的可变参数!

Catching the Exceptions/捕获异常

Catching the Exceptions/捕获异常

Same operands, different story!/同人不同命!

Same operands, different story!/同人不同命!

The out of scope variable/外部作用域变量

The out of scope variable/外部作用域变量

Beware of chained operations/小心链式操作

Beware of chained operations/小心链式操作

Name resolution ignoring class scope/忽略类作用域的名称解析

Name resolution ignoring class scope/忽略类作用域的名称解析

Needle in a Haystack/大海捞针

Needle in a Haystack/大海捞针

Section: The Hidden treasures!/隐藏的宝藏!

Section: The Hidden treasures!/隐藏的宝藏!

Okay Python, Can you make me fly?/Python,可否带我飞?

Okay Python, Can you make me fly?/Python,可否带我飞?

goto, but why?/goto,但为什么?

goto, but why?/goto,但为什么?

Brace yourself!/做好思想准备*

Brace yourself!/做好思想准备*

Let's meet Friendly Language Uncle For Life/让生活更美好*

Let's meet Friendly Language Uncle For Life/让生活更美好*

Even Python understands that love is complicated/连Python也知道爱是难言的

Even Python understands that love is complicated/连Python也知道爱是难言的

Yes, it exists!/是的,它存在!

Yes, it exists!/是的,它存在!

Inpinity/无限*

Inpinity/无限*

Mangling time!修饰时间!*

Mangling time!修饰时间!*

Section: Miscellaneous/杂项

Section: Miscellaneous/杂项

+= is faster/更快的+=

+= is faster/更快的+=

Let's make a giant string!/来做个巨大的字符串吧!

Let's make a giant string!/来做个巨大的字符串吧!

Explicit typecast of strings/字符串的显式类型转换

Explicit typecast of strings/字符串的显式类型转换

Minor Ones/小知识点

Minor Ones/小知识点

Contributing/贡献

Contributing/贡献

Acknowledgements/致谢

Acknowledgements/致谢

License/许可

License/许可

Help/帮助

Help/帮助

Want to surprise your geeky Pythonist friends?/想给你的极客朋友一个惊喜?

Need a pdf version?/需要来一份pdf版的?

Follow Commit/追踪Commit

本页网址:https://www.xinzhibang.net/article_detail-15883.html

寻求报道,请 点击这里 微信扫码咨询

关键词

Python 编程语言 特性

分享至微信: 微信扫码阅读

相关工具

相关文章

相关快讯