SICP-1.4-函数设计

何为好的函数

  • 每个函数只完成一个工作
  • 不要做重复工作
    • 如果你多次复制一段代码块,说明你应该进行函数抽象了
  • 定义一般化函数
    • 避免特例化
  • 函数说明
    • 一般通过三引号说明
    • help调出函数说明
  • 避免过多的参数
    • 设定参数默认值
  • def pressure(v, t, n=6.022e23):
            """Compute the pressure in pascals of an ideal gas.
    
            v -- volume of gas, in cubic meters
            t -- absolute temperature in degrees kelvin
            n -- particles of gas (default: one mole)
            """
            k = 1.38e-23  # Boltzmann's constant
            return n * k * t / v
posted @ 2017-06-19 20:48  elieyes  阅读(124)  评论(0编辑  收藏  举报