python:2017.11.3

来源:互联网 发布:筛选出的数据求和 编辑:程序博客网 时间:2024/05/17 09:38

函数

>>> def Hello(name):    'Hello+name'    return 'hello'+name>>> Hello.__doc__'Hello+name'>>> Hello('yc')('hello', 'yc')

1、当形参改变时,实参不改变

2、当参数为列表是,形参改变,实参也会改变


关键字参数

>>> def Hello(first, second):    return first+second>>> Hello(second="yc",first='Hello')'Helloyc'

1、将位置参数放在关键词参数前面可以同时使用


收集参数

>>> def test (*a):    return a>>> test(1,2,3)(1, 2, 3)
>>> def test (x,y,z,*a,**b):    print x    print y    print z    print a    print b>>> test(1,2,3,4,5,6,7,aa=1,bb=2)123(4, 5, 6, 7){'aa': 1, 'bb': 2}

1、收集其余位置的参数,没有时为空元组

2、**为字典

>>> def test (a,b):    return a+b>>> a=(1,2)>>> test(*a)3

1、调用函数时用*,分配列表中的数

2、**分配字典

全局变量

>>> x=1>>>> def test():    global x =x+1SyntaxError: invalid syntax>>> def test():    global x    x=x+1>>> test()>>> x2