本文地址: http://blog.csdn.net/caroline_wendy/article/details/17232035
函数(function)的基础知识, 包括参数,返回值, 默认参数(key parameter),关键值(key)参数,
全局(global)变量,非局部(nonlocal)变量, 可变参数(var args),函数文档(__doc__);
全局(global)变量: 可以修改全局的值;
非局部(nonlocal)变量: 可以在嵌套函数中修改上层函数的值;
关键值(key)参数: 可以通过关键值给函数的形参(parameter)赋值, 可以忽略位置的限定;
可变参数(var args): 可以参数匹配, 赋值给可变参数, 提供多个可变参数;
函数文档(__doc__): 可以输出函数的文档;
代码:
# -*- coding: utf-8 -*- #==================== #File: abop.py #Author: Wendy #Date: 2013-12-03 #==================== #eclipse pydev, python3.3 #函数 def sayHello(): print('Hello World') sayHello() sayHello() #带参数的函数 def printMax(a, b): if a > b: print(a, 'is maximum') #python自动生成空格 elif a == b: print(a, 'is equal to', b) else: print(b, 'is maximum') printMax(3, 4) x = 5 y = 7 printMax(x, y) #全部变量 num = 50 def func(): global num #全局变量, 不建议使用 print('num is', num) num = 2 print('Changed local num to', num) func() print('x is still', num) #非局部变量 def func_outer(): x = 2 print('x is', x) def func_inner(): nonlocal x #非局部变量, 函数内可见 x = 5 func_inner() print('Changed local x to', x) func_outer() #默认参数 def say(message, times = 1): print(message*times) say('Caroline') say('Wendy', 5) #关键参数, 指定参数 def func(a, b=5, c=10): print('a is', a, 'b is', b, 'c is', c) func(1) #a的值必须要指定 func(2, c=50) func(c = 100, a=5) #可变参数, *number是数组, **keywords是字典(map) def total(initial=5, *numbers, **keywords): count = initial for number in numbers: count += number for key in keywords: print(key) #打印key count += keywords[key] return count print(total(10, 4, 5, 6, Caroline = 50, Wendy = 100)) #返回值, 比较大小使用库函数max def maximum(x, y): if x>y: return x else: return y print(maximum(2, 3)) #表面return None def someFunction(): pass print(someFunction()) #函数文档 #格式: 首行以大写字母开始, 句号结尾, 次行空格, 接下来是描述 def Welcome(x, y): ''' Welcome to Caroline's world. Wendy and me will tell you how to write python. ''' print(x) print(y) Welcome('Caroline', 'Wendy') print(Welcome.__doc__) help(Welcome) #帮助信息
Hello World Hello World 4 is maximum 7 is maximum num is 50 Changed local num to 2 x is still 2 x is 2 Changed local x to 5 Caroline WendyWendyWendyWendyWendy a is 1 b is 5 c is 10 a is 2 b is 5 c is 50 a is 5 b is 5 c is 100 Wendy Caroline 175 3 None Caroline Wendy Welcome to Caroline's world. Wendy and me will tell you how to write python. Help on function Welcome in module __main__: Welcome(x, y) Welcome to Caroline's world. Wendy and me will tell you how to write python.