#__doc__属性
>>> sq.__doc__
'This is comment'
# help()函数用法
help(sq)
函数遇到return语句就会中断执行
没有return语句的函数,实际上函数返回了None值
>>> def test():
... 1 + 1
...
>>> print(test())
None
>>> def print_params(x, y, z=3, *pospar, **keypar):
... print(x, y, z)
... print(pospar)
... print(keypar)
...
>>> print_params(1, 2, 3, 4, 5, xx='girl', oo='girls')
(1, 2, 3)
(4, 5) # *pospar返回的是元祖
{'oo': 'girls', 'xx': 'girl'} # xx='girl', oo='girls'返回的是字典
>>> print_params(1, 2, 3, 4, 5, **{'xx':'girl', 'oo':'girls'}) #**{}方式
(1, 2, 3)
(4, 5)
{'oo': 'girls', 'xx': 'girl'}
>>> print_params(1, 2, xx='girl', oo='girls')
(1, 2, 3)
()
{'oo': 'girls', 'xx': 'girl'}
# **name
>>> def hello(**args):
... print('my name is', args['name'], ' age ', args['age'])
...
>>> hello(**a)
('my name is', 'f', ' age ', 23)
>>>
>>> a
{'age': 23, 'name': 'f'}
# *name
>>> def add(x, y):
... print(x+y)
...
>>> a=(1, 2)
>>>
>>> add(*a)
3
# 函数中局部变量
>>> def a(vars):
... print(vars + vars)
...
>>>
>>> vars='abc'
>>>
>>> a('test ')
test test
# globals()函数引用全局变量
>>> def b(vars):
... print(vars + globals()['vars'])
...
>>> b('test ')
test abc
#
>>> def c(c):
... print(c + vars)
...
>>> c('3c ')
3c abc
# 没有重新绑定全局变量
>>> def add(x):
... x = x + 1
... print(x)
...
>>> add(x)
2
>>> x
1
# global语句重新绑定全局变量值
>>> def add1():
... global x
... x = x + 1
... print(x)
...
>>>
>>> x
1
>>> add1()
2
>>> x
2
>>> def A(A):
... def B(B):
... return A * B
... return B
...
>>> qt = A(2)
>>> qt(5)
10
>>>
>>> qt = A(3)
>>> qt(3)
9
>>>
>>> qt(5)(6)
>>> A(5)(6)
30
>>> type(A(5))
'function'>
>>> type(A(5)(6))
'int'>
# map(func, seq) 对序列中的每个元素应用函数
>>> map(str, range(5))
['0', '1', '2', '3', '4']
# filter(func, seq) 返回函数执行序列中结果为真的值
>>> def func(x):
... return x.isalnum()
...
>>> seq = ['a', '1', '#']
>>> filter(func, seq)
['a', '1']
#使用列表解析
>>> [x for x in seq if x.isalnum()]
['a', '1']
#sum(seq) 将序列中的元素相加
>>> sum([1,2,3])
6