Python 命名约定指南 [英] Python naming conventions guide

查看:28
本文介绍了Python 命名约定指南的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想了解这种语言的命名约定,官方文档中有解释吗?如果没有,我如何介绍良好的 Python 实践?

I would like to learn aout the naming convetions in this language, this is explained in the official documentation?, if not how I could introduce to good Python practices?

推荐答案

如评论中提到的,可以参考官方 Python 风格指南 此处.命名约定位于此处.

As mentioned in the comments, you can reference the official Python style guide here. The naming conventions are here.

您还可以在此处查看 Google 的 Python 风格指南,其中包含命名约定在此处解决.

You can also check out Google's Python style guide here, with naming conventions addressed here.

以下是 Google 的一些亮点:

要避免的名称

  • 单个字符名称,计数器或迭代器除外
  • 任何包/模块名称中的破折号 (-)
  • __double_leading_and_trailing_underscore__ 名称(Python 保留)

命名约定

请注意,某些命名约定与 PEP8 不同,而是遵循本样式指南源自的原始 Google Python 样式指南.

Note that some naming conventions differ from PEP8 and instead follow the original Google Python Style guide from which this style guide originated.

  • 内部"是指模块内部或受保护或私有的一个班级.

  • "Internal" means internal to a module or protected or private within a class.

前置单个下划线 (_) 对保护模块变量和函数(不包含在 import * from 中).

Prepending a single underscore (_) has some support for protecting module variables and functions (not included with import * from).

在实例变量或方法前加上双下划线 (__)有效地使变量或方法对其私有类(使用名称修改).

Prepending a double underscore (__) to an instance variable or method effectively serves to make the variable or method private to its class (using name mangling).

将相关类和顶级函数放在一个模块中.与 Java 不同,没有必要将自己限制在每个类中模块.但是,请确保相同的模块具有高内聚性.

Place related classes and top-level functions together in a module. Unlike Java, there is no need to limit yourself to one class per module. However, make sure the classes and top-level functions in the same module have high cohesion.

类名使用 CapWords,模块使用lower_with_under.py名称.命名示例

Use CapWords for class names, but lower_with_under.py for module names. Naming examples

这篇关于Python 命名约定指南的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆