Python:函数

来自KlniuWiki
跳转到: 导航, 搜索

1 基础

函数的参数传递有以下几个规则:

  1. Arguments are passed by automatically assigning objects to local variable names.
  2. Assigning to argument names inside a function does not affect the caller.
  3. Changing a mutable object argument in a function may impact the caller.
  4. Immutable arguments are effectively passed “by value.”
  5. Mutable arguments are effectively passed “by pointer.”

2 设计函数

函数的设计及使用有以下几条建议:

  • Coupling: use arguments for inputs and return for outputs.
  • Coupling: use global variables only when truly necessary.
  • Coupling: don’t change mutable arguments unless the caller expects it.
  • Cohesion: each function should have a single, unified purpose.
  • Size: each function should be relatively small.
  • Coupling: avoid changing variables in another module file directly.
个人工具
分类
化学
[×] 國學
学佛
[×] 数学
物理
生活
[×] 英语
读书
辞典
廣告