Python 社区普遍采用小写字母加下划线的命名方式(snake_case),这种风格使代码更具可读性。 1.1.1 示例 # 正确的命名方式 user_name = "Alice" get_user_info() # 不推荐的命名方式 userName = "Alice" # 驼峰命名法(CamelCase)在 Python 中不常用 GetUserInfo() # 类似于类名的命名方式 1.2 类命名 类名...
import unittest def average(seq): return sum(seq) / len(seq) class TestAverage(unittest.TestCase): def test_zero(self): self.assertRaises(ZeroDivisionError, average, []) def test_with_zero(self): with self.assertRaises(ZeroDivisionError): average([]) if __name__ == "__main__": unittes...
不要将制表符和空格混合使用。 IDEL和Emacs的Python的都支持 spaces模式。 每个函数之间应该有一个空行。 每一个 Class 之间应该有两个空行。 空格(行)使用 (2) 在使用 字典(dict), 列表(list), 元组(tuple), 参数(argument)列表时, 应在 "," 前添加一个空格, 并且使用字典(dict)时,在 ":" 号后添加...
confusing both readers and many automated tools. There is one defensible use case for a wildcard import, which is to republish an internal interface as part of a public API (for example, overwriting a pure Python implementation of an interface with...
Tabs or Spaces|制表符还是空格 空格是首选的缩进方法。 制表符应仅用于与已使用制表符缩进的代码保持一致。Python 不允许混合制表符和空格进行缩进。 Maximum Line Length|最大代码行长度 限制所有行的最大长度为79个字符。 对于较少结构限制的长文本块(例如文档字符串或注释),行长度应限制为72个字符。
变量名、函数名应使用小写字母和下划线(snake_case)。 类名使用驼峰命名法(CapWords或CamelCase)。 常量全大写,单词间用下划线分隔。 导入语句 导入语句应放在文件开头,先标准库导入,后第三方库导入,最后是本地应用/模块导入。 每个导入语句应单独一行,可以使用括号来分组多条导入语句。
Python program to convert a String to camelCase # importing the modulefromreimportsub# function to convert string to camelCasedefcamelCase(string):string=sub(r"(_|-)+"," ",string).title().replace(" ","")returnstring[0].lower()+string[1:]# main codes1="Hello world"s2="Hello,world...
camelCase 只有在预先制定好的命名规范使用 属性: interface, _internal, __private 但尽量避免__private形式。 较长代码行 保持一行代码在 80 个字符长度。 在括号内使用隐含的行延续: Python代码 def __init__(self, first, second, third, ...
lower_case_with_underscores(下划线分隔的小写字母) UPPERCASE(大写字母) UPPER_CASE_WITH_UNDERSCORES(下划线分隔的大写字母) CapitalizedWords(或CapWords,或CamelCase,因为其字母看起来有点崎岖[4])。这有时也被称为StudlyCaps。 注意:在CapWords中使用首字母缩写时,请将缩写的所有字母都大写。因此,HTTPServerError比...
大写单词, 也称为CapWords, CamelCase或StudlyCaps。 混合情况 大写的单词_带_下划线 _single_leading_underscore:”内部使用”指标较弱。例如, 从M import *不会导入名称以下划线开头的对象。 single_trailing_underscore_:按惯例使用, 以避免与Python关键字发生冲突, 例如Tkinter.Toplevel(master, class _ =’Class...