博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Python中dunder名称的来历
阅读量:4330 次
发布时间:2019-06-06

本文共 2073 字,大约阅读时间需要 6 分钟。

在 Python 中,我们经常会看到被双下划线包围的属性名和方法名,比如下面代码中的 __future__, __all__, __version__, __author__。初看还真奇怪啊,感觉别扭得很。

"""This is the example module.This module does stuff."""from __future__ import barry_as_FLUFL__all__ = ['a', 'b', 'c']__version__ = '0.1'__author__ = 'Cardinal Biggles'import osimport sys

还有比长相更别扭的,就是怎么念它们。

这种风格的名字在 Python 中有很多的名字,最开始叫魔法变量魔法函数 (magic)。

加两个下划线,这个名字就带有魔法了?

相信有很多人和我一样,把__all__念作magic all,就好像指着一匹马,我却要说牛一样。

所以后面有个人提议,把带双下划线的属性名和方法名叫dunders。大家都觉得很好,就慢慢流行起来了。在 Python 的官方的 《 Python 代码样式指南》(PEP 8 -- Style Guide for Python Code [1]) 中,就把双下划线的名字叫做dunders

magic我还知道是魔法。我堂堂过了英语六级的人,看到dunders直接蒙圈了。有道词典一查,解释是 1). 甘蔗渣; 2). (Dunder)人名.。 难道是一个叫 Dunder 的人发明了双下划线命名法,为了纪念他,把带双下划线的名字都叫做 duner? 双下划线命名也有一个发明人?这太扯了。

后面有 bing 搜索(英文搜索, bing 比 baidu 好些, google 被墙用不了),终于在 Python 官方的 wiki 中找到dunders的真正来历了 [3]。

原来 dunderDouble UNDERscore(中文双下划线)的缩写,分别取DoubleDUnderscoreUnder组成。这样取名后方便发音。

在发明 dunder 之前, __init__ 要念作 double underscore init, 其中的double underscore 有17个单词,发音是6声(嘴或舌头要变换7次动作,你可以以试试),而dunder 只有6个单词,发音是2声。大大降低了手和嘴的劳动量,这个好的发明,当然举双手占成了。

Python 官方 wiki 引用如下:

Dunder (Double UNDERscore) AliasMark Jackson was the first to suggest dunder as a speech shorthand for double underscores (__) in a reply to a query from Pat Notz. Ned Batchelder later stressed the need for a way of pronouncing __:An awkward thing about programming in  Python : there are lots of double underscores. [snip] My problem with the double underscore is that it's hard to say. How do you pronounce __init__? "underscore underscore init underscore underscore"? "under under init under under"? Just plain "init" seems to leave out something important. I have a solution: double underscore should be pronounced "dunder". So __init__ is "dunder init dunder", or just "dunder init".

顺便给有道词典提了个建议,把dunder在 Python 中的解释也加到词库中。

参考

[1]

[2]

[3]

版权声明:本文为作者原创,允许转载,但必须注明原文地址:


本文的数字签名如下:

MGUCMGkxQcQNBCyoaOxWL0OJ0egTdwWs10aSObVMEW/+owscntk9/xXsmFThQNdZ28IePwIxAOrzdI3+A2WJdmxDOHuZDOpaLQBiPhfCkvSfuqC9ph/di23XR3hk0vqiYXQ5lSgWjg==

--- 2019-7-20 9:34:19

转载于:https://www.cnblogs.com/byronsh/p/10741084.html

你可能感兴趣的文章
阶段3 2.Spring_04.Spring的常用注解_3 用于创建的Component注解
查看>>
阶段3 2.Spring_04.Spring的常用注解_2 常用IOC注解按照作用分类
查看>>
阶段3 2.Spring_09.JdbcTemplate的基本使用_5 JdbcTemplate在spring的ioc中使用
查看>>
阶段3 3.SpringMVC·_07.SSM整合案例_02.ssm整合之搭建环境
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第1节零基础快速入门SpringBoot2.0_3、快速创建SpringBoot应用之手工创建web应用...
查看>>
阶段3 3.SpringMVC·_07.SSM整合案例_04.ssm整合之编写SpringMVC框架
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第1节零基础快速入门SpringBoot2.0_5、SpringBoot2.x的依赖默认Maven版本...
查看>>
阶段3 3.SpringMVC·_07.SSM整合案例_08.ssm整合之Spring整合MyBatis框架
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第2节 SpringBoot接口Http协议开发实战_9、SpringBoot基础HTTP其他提交方法请求实战...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第2节 SpringBoot接口Http协议开发实战_12、SpringBoot2.x文件上传实战...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第4节 Springboot2.0单元测试进阶实战和自定义异常处理_19、SpringBoot个性化启动banner设置debug日志...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第4节 Springboot2.0单元测试进阶实战和自定义异常处理_20、SpringBoot2.x配置全局异常实战...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第5节 SpringBoot部署war项目到tomcat9和启动原理讲解_23、SpringBoot2.x启动原理概述...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第4节 Springboot2.0单元测试进阶实战和自定义异常处理_21、SpringBoot2.x配置全局异常返回自定义页面...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第8节 数据库操作之整合Mybaties和事务讲解_32..SpringBoot2.x持久化数据方式介绍...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第8节 数据库操作之整合Mybaties和事务讲解_34、SpringBoot整合Mybatis实操和打印SQL语句...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第8节 数据库操作之整合Mybaties和事务讲解_35、事务介绍和常见的隔离级别,传播行为...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第9节 SpringBoot2.x整合Redis实战_40、Redis工具类封装讲解和实战...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第9节 SpringBoot2.x整合Redis实战_37、分布式缓存Redis介绍...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第10节 SpringBoot整合定时任务和异步任务处理_42、SpringBoot常用定时任务配置实战...
查看>>