格式:
def 函数名 (参数):
内容 / pass;# pass 为占位符, 空
return; / None;
一, 普通函数
- #!/usr/bin/python
- def my_abs(x): #空函数
- None;
- def my_pass(): #空函数
- pass;
- def my_print(x):
- print "haha"
- def my_try(a):
- return None;
二, 函数参数检查: isinstance(参数, 参考的 tuple)
- def your_age(x)
- if not isinstance(x, (int, float)): #检查参数 x 是否为 tuple 中的 int/float 类型
- raise TypeError('the age's type is error, please check again'); #出错信息打印, 类似于 C 的 perror();
- if x <18:
- print 'teenager';
- if x>= 18:
- print 'adult';
- return None;
三, 返回多个值
- def my_abs(a, b)
- ret1 = abs(a);
- ret2 = abs(b);
- return ret1, ret2; #其实返回值是 tuple 类型, ret1,re2 是其元素;
来源: http://www.bubuko.com/infodetail-2687059.html