# Python学习笔记--函数

http://www.liaoxuefeng.com/

Python内置了很多函数，可以直接调用。Python内置的函数可以通过官方文档查看。也可以通过help()查看帮助信息。

# 变量a为函数abs()的别名
a = abs
a(-1) #结果为1

### 1. 定义函数

#自定义取绝对值函数
def my_abs(x):
#isinstance()检查参数类型
if not isinstance(x, (int, float)):
if x >= 0:
return x
else:
return -x

>>> from function import my_abs
>>> print(my_abs(-1))
1
>>> print(my_abs(1))
1

pass语句可以用做占位符，表示什么都不做，可用来定义一个空函数。在没想好具体语句的时候，可以用pass语句使代码先运行起来。

#定义空函数
def nop():
pass
#放在if语句中
if a > b:
pass

### 2. 默认参数

def enroll(name, gender, age = 7, city = 'Beijing'):
print('name = ', name, '\tgender = ', gender, '\tage = ', age, '\tcity = ', city)

enroll('Jim', 'M')
enroll('Lily', 'F', 6, 'Tianjin')
enroll('Tom', 'M', 8)
enroll('Jack', 'M', city = 'Xi\'an')

name =  Jim     gender =  M     age =  7    city =  Beijing
name =  Lily    gender =  F     age =  6    city =  Tianjin
name =  Tom     gender =  M     age =  8    city =  Beijing
name =  Jack    gender =  M     age =  7    city =  Xi'an

def add_end(L = []):
L.append('end')
return L

#正常调用
#使用默认参数调用
print(add_end())

[1, 2, 3, 'end']
['a', 'b', 'c', 'end']

['end']
['end', 'end']

def add_end(L = None):
if L is None:
L = []
L.append('end')
return L
#使用默认参数调用
print(add_end())

['end']
['end']

### 3. 可变参数

def add_sum(*nums):
sum = 0
for num in nums:
sum += num
return sum

L = [1, 2, 3, 4]
T = (1, 2)
print(add_sum(*T))

6
10
3

### 4. 关键字参数

def person(name, age, city = 'Beijing', country = 'china', **kw):
print('name: ', name, '\tage: ', age, '\tcity: ', city, '\tcountry: ', country, '\tother: ', kw)
#dict
other = {'gender': 'F', 'height': 168}
person('Tom', 18)
person('Lily', 20, city = 'Tianjin', gender = 'F', hobby = 'read')
person('Tim', 15, gender = 'M', city = 'Beijing')
#将dict作为关键字参数传入
person('Ann', 16, **other)

name:  Tom  age:  18    city:  Beijing  country:  china     other:  {}
name:  Lily     age:  20    city:  Tianjin  country:  china     other:  {'gender': 'F', 'hobby': 'read'}
name:  Tim  age:  15    city:  Beijing  country:  china     other:  {'gender': 'M'}
name:  Ann  age:  16    city:  Beijing  country:  china     other:  {'gender': 'F', 'height': 168}

### 5. 命名关键字参数

def person(name, age, *, city, country):
print('name: ', name, '\tage: ', age, '\tcity: ', city, '\tcountry: ', country)
#如果已经有可变参数，不需要特殊分隔符*
def person1(name, age, *args, city, country):
print('name: ', name, '\tage: ', age, '\tcity: ', city, '\tcountry: ', country)

person('Tom', 18, city = 'Tianjin',country = 'china')
person('Tom', 18, country = 'china', city = 'Tianjin')
person1('Tom', 18, city = 'Tianjin',country = 'china')
person1('Tom', 18, country = 'china', city = 'Tianjin')

### 6. 组合参数

def func1(a, b, c = 0, *d, e, **kw):
print(a, '\t', b, '\t', c, '\t', d, '\t', e, '\t', kw)

def func2(a, b, c = 0, *, d, e, **kw):
print(a, '\t', b, '\t', c, '\t', d, '\t', e, '\t', kw)

def func3(a, b, c):
print(a, '\t', b, '\t', c)

t = (3, 4, 5, 6, 7, 8, 9)
l = [3, 4, 5, 6, 7, 8, 9]
t1 = (4, 5, 6)
dic = {'name': 'Z', 'gender': 'M'}
print('func1')
func1(1, 2, e = 4, d = 3)
func1(1, 2, d = 3, e = 4)
func1(1, 2, d = 3, e = 4, name = 'L', gender = 'F')
func1(*t, e = 10)
func1(*l, e = 10)
func1(*t, e = 11, **dic)
func1(*l, e = 11, **dic)
print('func2')
func2(1, 2, d = 3, e = 4)
func2(1, 2, d = 3, e = 4, name = 'L', gender = 'F')
func2(*t1, d = 20, e = 20)
func2(*t1, d = 21, e = 21, **dic)
func3(*t1)

func1
1    2   0   ()      4   {'d': 3}
1    2   0   ()      4   {'d': 3}
1    2   0   ()      4   {'name': 'L', 'gender': 'F', 'd': 3}
3    4   5   (6, 7, 8, 9)    10      {}
3    4   5   (6, 7, 8, 9)    10      {}
3    4   5   (6, 7, 8, 9)    11      {'name': 'Z', 'gender': 'M'}
3    4   5   (6, 7, 8, 9)    11      {'name': 'Z', 'gender': 'M'}
func2
1    2   0   3   4   {}
1    2   0   3   4   {'name': 'L', 'gender': 'F'}
4    5   6   20      20      {}
4    5   6   21      21      {'name': 'Z', 'gender': 'M'}
4    5   6

func1中，
d为可变参数，可以为空，d=3被认为是关键字参数；
e为命名关键字参数，调用时必须使用名字调用；

func2中，
d, e均为命名关键字，必须使用名字调用。