#callback 判断是否存在
import match
x = 1
y = math.sqrt
callable(x)
#False
callable(y)
#True
#函数
def fib(num):
result = [0,1]
for i in range(num-2):
result.append(result[-2],result[-1])
return result;
fibs(10)
#[0,1,1,2,3,5,8,13,21,34]
#函数文档
def square(x):
'Calcutates the square of the number x'
return x*x
square.__doc__
#'Calcutates the square of the number x'
#None
def test():
print 'This is printed'
return
print 'This is not'
x = test()
#This is printed
print x
#None
#改变列表
def change(n):
n[0] = 'Mr.Gumby'
names = ['Mr.Entiy','Mrs.Thing']
change(names)
print names
#['Mr,Gumby','Mrs.Thing']
#复制列表
names = ['1','2']
n = names[:]
n is names
#False
n == names
#True
#位置参数
def hello_1(greeting,name):
print '%s,%s!' % (greeting,name)
def hello_2(name,greeting):
print '%s,%s!' % (name,greeting)
hello_1('Hello','World')
#Hello,World
hello_2('Hello','World')
#Hello,World
hello_2(greeting='Hello',name='World')
#World,Hello
#收集参数
def print_params(title,*params):
print title
print params
print_params('params:',1,2,3)
#params:
#(1,2,3)
def print_params2(title,**params):
print title
print params
print_params2('params:',x=1,y=2,z=3)
#{'z':3,'x':1,'y':2}