函数

# def test(x):
# '''
# 2*x+1
# :param x:整形数字
# :return: 返回计算结果
# '''
# y=2*x+1
# return y
#
# def test():
# '''
# 2*x+1
# :param x:整形数字
# :return: 返回计算结果
# '''
# x=3
# y=2*x+1
# return y
# print (test)打印函数地址
# a=test()
# print(a)打印返回值



#过程:就是没有返回值的函数
# def test01():
# msg = 'test01'
# print(msg)
#
#
# def test02():
# msg = 'test02'
# print(msg)
# return msg
#
# return可返回多少个值,以元组的形式返回
# def test03():
# msg = 'test03'
# print(msg)
# return 1,2,3,4,'a',['alex'],{'name':'alex'},None
#
# def test04():
# msg = 'test03'
# print(msg)
# return {'name':'alex'}
# t1=test01()
# t2=test02()
# t3=test03()
# t4=test04()
# print(t1)
# print(t2)
# print(t3)
# print(t4)
# >>>
# test01
# test02
# test03
# test03
# None
# test02
# (1, 2, 3, 4, 'a', ['alex'], {'name': 'alex'}, None)
# {'name': 'alex'}

# 没有返回值,则返回None
# 返回值=1,返回object
# 返回值>1,返回tuple

位置参数:形参实参位置一一对应,缺少或多出都不行

def test(x,y,z)
    print(x)
    print(y)
    print(z)

test(2,4,7)

关键字参数:位置无需固定,以关键字为依据调用,缺少或多出都不行

def test(x,y,z)
    print(x)
    print(y)
    print(z)

test(x=2,z=4,y=7)

位置参数必须在关键字参数左边

def test(x,y,z)
    print(x)
    print(y)
    print(z)
test(1,y=2,3)#报错
 test(1,3,y=2)#报错
 test(1,3,z=2)
 test(1,3,z=2,y=4)#报错
 test(z=2,1,3)#报错

默认参数:定义函数时默认有的值的参数

 def handle(x,type='mysql'):
     print(x)
     print(type)

 handle('hello')
 handle('hello',type='sqlite')
 handle('hello','sqlite')

安装程序,默认通过的函数:

 def install(func1=False,func2=True,func3=True):
     pass

 

#参数组:**字典 *列表
参数组:**字典 *列表
def test(x,*args):
    print(x)
    print(args)


test(1)
test(1,2,3,4,5)
test(1,{'name':'alex'})
test(1,['x','y','z'])
test(1,*['x','y','z'])
test(1,*('x','y','z'))

def test(x,**kwargs):
    print(x)
    print(kwargs)
test(1,y=2,z=3)
test(1,1,2,2,2,2,2,y=2,z=3)#会报错
test(1,y=2,z=3,z=3)#会报错 :一个参数不能传两个值

def test(x,*args,**kwargs):
    print(x)
    print(args,args[-1])
    print(kwargs,kwargs.get('y'))
test(1,1,2,1,1,11,1,x=1,y=2,z=3) #报错
test(1,1,2,1,1,11,1,y=2,z=3)

test(1,*[1,2,3],**{'y':1})

 

posted on 2018-07-24 22:46  shlvst  阅读(99)  评论(0编辑  收藏  举报

导航