📂
CS-NoteBook
  • Introduction
  • CS144
    • concise introduction to Internet
      • 1.1 Networked Applications
      • 1.2 The 4 Layer Internet
      • 1.3 IP
      • 1.4 A Day in the Life of a Packet
      • 1.5 Principle: Packet Switching
      • 1.6 Principle:Layering
      • 1.7 Principle: Encapsulation
      • 1.8 Byte order and packet formats
      • 1.9 name and addresses:IPv4
      • 1.10 Longest Prefix Match for Link Layer
      • 1.11 Address Resolution Protocol(ARP)
      • 1.12 Summary
    • Transport Layer
      • 2.1 The TCP Service Model
      • 2.2 UDP service model
      • 2.3 ICMP(Internet Control Message Protocol 互联网报文控制协议)
      • 2.4 The End-to-End Principle
      • 2.5 Error Detection:3 schemes (Checksum,CRC and MAC)
      • 2.6 Finite State Machines(有限状态机)
      • 2.7 Flow Control
      • 2.8 Sliding window
      • 2.9 Retransmission Strategies
      • 2.10 TCP Header
      • 2.11 TCP Setup and Teardown
      • 2.12 Recap
    • Package Switching
      • 3.1 The history of Internet
      • 3.2 What is packet switching
      • [3.3 End-to-end delay and Queueing delay
      • 3.4 Playback Buffer(回放缓存区)
  • CS 61C
    • 1.4 C Memory Mangement, Usage
    • 1.5 Intro to Assembly Language, MIPS Intro
    • 1.5 extra bits operation
  • CS 61B
  • CS 61A
    • Function
    • Names
    • The Art of the Function
    • Control
    • Higher-Order Function
    • Recursive Function
    • List
    • Non-Local Assignment
    • Iterators
    • Objects
    • Data Abstraction
    • OOP
    • Inheritance
    • Representations
    • Decomposition
    • Scheme
    • Exceptions
    • Calculator
    • Interpreters
    • Declarative_Programming
    • Table
    • Aggregation
      • More_recursion
    • Databases
    • Distributed_Data
    • Tail Recursion
    • Exercises
      • lab00
      • lab01
      • hw01
      • tree Recursion example -- give Change
  • The Web DevelopMent Bootcamp
    • html5
    • css
    • bootstrap3
    • bootstrap4
    • javascript expression
    • javascript function
Powered by GitBook
On this page
  • 如何编写良好的函数
  • 文档字符串
  • 参数默认值
  • python 可以有多个 return 值

Was this helpful?

  1. CS 61A

The Art of the Function

PreviousNamesNextControl

Last updated 4 years ago

Was this helpful?

如何编写良好的函数

  • 如何编写良好的函数

    • 每个函数都应该只做一个任务

      • 任务使用短小的名称来定义,使用一行文本来标识

    • 不要重复劳动(DRY)是软件工程的中心法则。

      • 多个代码段不应该描述重复的逻辑

    • 函数应该定义得泛型

      • 任何情况下能可以使用

文档字符串

  • 描述这个函数的文档,叫做文档字符串

    • 使用三个引号

      • 第一行描述函数的任务

      • 描述参数

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

help(pressure) # 查看文档字符串

参数默认值

k_b=1.38e-23  # Boltzmann's constant
def pressure(v, t, n=6.022e23):
    return n * k_b * t / v
  • 函数体的大多数数据

    • 应该表示为具名参数的默认值

  • 永远不会改变的值

    • 就像基本常数k_b,应该定义在全局帧中。

python 可以有多个 return 值

def divide_exact(n,d):
    return n // d, n % d
quotient, remainder = divide_exact(2013, 10)
>>> quotient
201
>>> remainder 
3
  • 实际上返回的是一个元组 tuple

文档字符串准则