Python 代码风格 & Python coding style

本文介绍了Python编程的基本规范,包括缩进、代码长度限制、注释、文档字符串等,并强调了类和函数命名的一致性,以及在国际环境中使用编码时的注意事项。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

from 《Python 2.7.9 documentation》

- Use 4-space indentation, and no tabs.  
使用4空格缩进,不要使用tab缩进

- Wrap lines so that they don’t exceed 79 characters.  
拆行确保每行不超过79个字符

- Use blank lines to separate functions and classes, and larger blocks of code inside functions.  
函数和类之间用空行隔开,包括大块代码段也用空行隔开

- When possible, put comments on a line of their own.  
注释单独占一行

- Use docstrings.  
使用文档字符串

- Use spaces around operators and after commas, but not directly inside bracketing constructs: a = f(1, 2) + g(3, 4).  
操作符两侧和逗号后面留空格,但括号里侧不加空格:a = f(1, 2) + g(3, 4)

- Name your classes and functions consistently; the convention is to use CamelCase for classes and lower_case_with_underscores for functions and methods. Always use self as the name for the first method argument (see A First Look at Classes for more on classes and methods).  
统一类名和函数名,推荐类名用驼峰命名:SampleClass,函数名用小写和下划线命名:get_page(),尽量用self作为方法的第一个参数

- Don’t use fancy encodings if your code is meant to be used in international environments. Plain ASCII works best in any case.
国际化环境中慎用编码,ASCII最佳。(中文环境下,推荐utf-8)

转载于:https://www.cnblogs.com/GeekRai/p/4660955.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值