在python中,命名规范是编写清晰,可读性强代码的重要部分,遵循这些规范可以使代码更易于理解和维护。
Type | 命名约定 | 命名例子 |
函数(Function) | 小写单词,下划线分割单词 | function,delta_function |
方法(Method) | 小写单词,下划线分割单词 | what_a_method,fetch |
变量(Variable) | 小写单词,下划线分割单词 | x,hero,shopping_cart |
常量(Constant) | 大写单词,下划线分隔单词 | MAX_SIZE,TOP_LEVEL |
类(Class) | 首字母大写,不用下划线分割单词 | UserProfile,Order |
模块(Module) | 简短小写单词,下划线分隔单词 | user_profile.py,file_h |
包(Package) | 简短小写单词,不用下划线分割单词 | user,userprofile,socialmedia |