2018-09-14

textbook 1.3

  • python執行順序從左到右
  • 函數調用時的執行順序
  • 變量的作用域
  • 把要實現的內容抽象成一個個子功能函數
  • /是除法,//是整除

textbook 1.4

  • Docstrings說明註釋用來說明函數,三個單引號,和函數體縮進一致,help(函數)時,直接出現函數說明
>>> def pressure(v, t, n):
        """Compute the pressure in pascals of an ideal gas.

        Applies the ideal gas law: http://en.wikipedia.org/wiki/Ideal_gas_law

        v -- volume of gas, in cubic meters
        t -- absolute temperature in degrees kelvin
        n -- particles of gas
        """
        k = 1.38e-23  # Boltzmann's constant
        return n * k * t / v
When you call help with the name of a function as an argument, you see its docstring (type q to quit Python help).

>>> help(pressure)
  • 設計函數時:函數的功能單一,函數儘量的通用,函數內容不要多次重複,需要多次重複的地方應該抽象成一個函數。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章