6. function

2016-10-15  本文已影响0人  捕鲸小能手

Function definition

def foo(x, y):
    print(x, y)
    return x, y

x, y = foo(1, 2)
print(x, y)

Default parameters

def foo(bar='bar', baz='baz'):
    print(bar, baz)

foo('x', 'y')
foo(bar='x')
foo(baz='y')

Variable number of arguments

def foo(*args):
    print(args)

def bar(**kwargs):
    print(kwargs)

foo(1, 2, 3)
foo(*[1, 2, 3])

bar(x=1, y=2)
bar(**{'x': 1, 'y': 2})
上一篇 下一篇

猜你喜欢

热点阅读