Python:函数
来自KlniuWiki
1 基础
函数的参数传递有以下几个规则:
- Arguments are passed by automatically assigning objects to local variable names.
- Assigning to argument names inside a function does not affect the caller.
- Changing a mutable object argument in a function may impact the caller.
- Immutable arguments are effectively passed “by value.”
- 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.