Python 变量和方法的正确大小写和格式

发布于 2024-08-10 23:39:50 字数 240 浏览 4 评论 0原文

所以我知道有些语言有预期的约定。

PHP - underscore_case() [大部分情况下,lolo]

Java - camelCase()

C# - PascalCase()

等。

什么是“Pythonic” “命名约定?我知道这最终并不重要,只是想知道是否有大多数模块完成的“最佳实践”方式。

So I know some languages have expected conventions.

PHP - underscore_case() [for the most part, lolo]

Java - camelCase()

C# - PascalCase()

etc.

What's the "Pythonic" naming convention? I know it doesn't matter in the end but just wondering if there is a "best practice" way that most modules are done in.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

格子衫的從容 2024-08-17 23:39:50

两个词:PEP 8

PEP 8 是(事实上的)Python 风格指南。本文档中的一些亮点(我故意遗漏了一些内容;请阅读原始文档以了解详细信息):

  • 包和模块名称:全小写名称。如果可以提高可读性,可以在模块名称中使用下划线。

  • 类名称:类名称几乎毫无例外地使用 CapWords 约定。*

  • 全局变量名称:约定与函数大致相同。

  • 函数名称:函数名称应小写,必要时用下划线分隔单词以提高可读性。仅在已成为流行风格的上下文中(例如 threading.py)才允许混合大小写,以保持向后兼容性。

  • 方法名称和实例变量:小写,单词之间用下划线分隔,以提高可读性。仅对非公共方法和实例变量使用一个前导下划线。

  • 常量:全部用大写字母书写,并用下划线分隔单词。示例包括。

Two words: PEP 8.

PEP 8 is the (de facto) Python style guide. Some highlights from this document (I left some stuff out on purpose; go read the original document for the ins and outs):

  • Package and Module Names: All-lowercase names. Underscores can be used in the module name if it improves readability.

  • Class Names: Almost without exception, class names use the CapWords convention.*

  • Global Variable Names: The conventions are about the same as those for functions.

  • Function Names: Function names should be lowercase, with words separated by underscores as necessary to improve readability. mixedCase is allowed only in contexts where that's already the prevailing style (e.g. threading.py), to retain backwards compatibility.

  • Method Names and Instance Variables: Lowercase with words separated by underscores as necessary to improve readability. Use one leading underscore only for non-public methods and instance variables.

  • Constants: Written in all capital letters with underscores separating words. Examples include.

寄居人 2024-08-17 23:39:50

阅读PEP 8

它是 Python 代码的风格指南,由 Python 的创建者 Guido van Rossum 编写。

顺便说一句,您问题的答案是对变量和函数名称使用 underscore_case ,对类使用 PascalCase

Read PEP 8.

It's a style guide for Python code, written by Python's creator, Guido van Rossum.

Incidentally, the answer to your question is to use underscore_case for variables and function names, and PascalCase for classes.

风铃鹿 2024-08-17 23:39:50

七个字:Google 代码之夏 Python 风格指南

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

  • “内部”是指模块内部或类中受保护或私有的。
    前面添加一个下划线 (_) 对保护模块变量和函数有一定的支持(不包含在 import * from 中)。
  • 在实例变量或方法前面添加双下划线 (__),可以有效地使该变量或方法对其类私有(使用名称修改)。
  • 将相关的类和顶级函数放在一个模块中。与 Java 不同,无需将自己限制为每个模块一个类。但是,请确保同一模块中的类和顶级函数具有高内聚性。
  • 使用 CapWords 作为类名称,使用 lower_with_under.py 作为模块名称。

命名示例

  • 软件包:lower_with_under
  • 模块:lower_with_under_lower_with_under
  • 类:CapWords、 _CapWords
  • 例外:CapWords
  • 函数:firstLowerCapWords()_firstLowerCapWords()
  • 全局/类常量:CAPS_WITH_UNDER_CAPS_WITH_UNDER
  • 全局/类变量:lower_with_under_lower_with_under
  • 实例变量:lower_with_under_lower_with_under(受保护)或 __lower_with_under(私有)
  • 方法名称:firstLowerCapWords()_firstLowerCapWords()(受保护)或 __firstLowerCapWords()(私有)
  • 函数/方法参数:lower_with_under
  • 局部变量:lower_with_under

Seven words: Google Summer of Code Python Style Guide

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.
    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).
  • 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.
  • Use CapWords for class names, but lower_with_under.py for module names.

Naming examples

  • Packages: lower_with_under
  • Modules: lower_with_under, _lower_with_under
  • Classes: CapWords, _CapWords
  • Exceptions: CapWords
  • Functions: firstLowerCapWords(), _firstLowerCapWords()
  • Global/Class Constants: CAPS_WITH_UNDER, _CAPS_WITH_UNDER
  • Global/Class Variables: lower_with_under, _lower_with_under
  • Instance Variables: lower_with_under, _lower_with_under (protected) or __lower_with_under (private)
  • Method Names: firstLowerCapWords(), _firstLowerCapWords() (protected) or __firstLowerCapWords() (private)
  • Function/Method Parameters: lower_with_under
  • Local Variables: lower_with_under
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文