Python代码风格(documentation部分待更)

Python代码风格

Python风格规范-谷歌开源项目风格指南

最近发现了一个不错的参考网站


官方的Style Guide for Python Code.

有关编写有用的文档字符串的准则,请参见:Docstring Conventions.

本文摘自以上两个网站以及《Data Structures and Algorithms in Python》

  • Python code blocks are typically indented by 4 spaces.
  • Use meaningful names for identifiers.
    • Classes (other than Python’s built-in classes)
      • a singular noun, and should be capitalized (e.g., Date rather than date or Dates).
      • When multiple words are concatenated to form a class name (e.g., CreditCard).
    • Functions, including member functions of a class
      • should be lowercase.
      • If multiple words are combined, they should be separated by underscores (e.g., make_payment).
      • should typically be a verb that describes its affect.
      • However, if the only purpose of the function is to return a value, the function name may be a noun that describes the value (e.g., sqrt rather than calculate_sqrt).
    • Names that identify an individual object (e.g., a parameter, instance variable, or local variable)
      • should be a lowercase noun (e.g., price).
      • Occasionally, we stray from this rule when using a single uppercase letter to designate the name of a data structures (such as tree T).
        - Identifiers that represent a value considered to be a constant
      • using all capital letters and with underscores to separate words (e.g., MAX_SIZE).
      • Recall from our discussion of encapsulation that identifiers in any context that begin with a single leading underscore (e.g., _secret) are intended to suggest that they are only for “internal” use to a class or module, and not part of a public interface.
  • Comments
    • In-line comments are good for quick explanations
      if n % 2 == 1: # n is odd
    • Multiline block comments are good for explaining more complex code sections. In Python, these are technically multiline string literals, typically delimited with triple quotes (”””), which have no effect when executed. In the next section, we discuss the use of block comments for documentation.

小结

Types of IdentifiersExamples
Classes (other than Python’s built-in classes)Date , CreditCard
Functions, including member functions of a classmake_payment, sqrt
Names that identify an individual object (e.g., a parameter, instance variable, or local variable)price, a tree T
Identifiers that represent a constantMAX_SIZE
Identifiers that are only for “internal” use to a class or module, and not part of a public interface_secret
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值