1
0
mirror of https://github.com/adambard/learnxinyminutes-docs.git synced 2025-08-12 01:34:19 +02:00

Functions section

This commit is contained in:
Geoff Liu
2014-11-04 13:15:57 -07:00
parent 4fb5839edc
commit ce381ce958

View File

@@ -406,24 +406,22 @@ add(5, 6) # => 印出"x is 5 and y is 6"并且返回11
add(y=6, x=5) # 关键字参数可以用任何顺序 add(y=6, x=5) # 关键字参数可以用任何顺序
# You can define functions that take a variable number of # 我们可以定义一个可变参数函数
# positional arguments
def varargs(*args): def varargs(*args):
return args return args
varargs(1, 2, 3) # => (1, 2, 3) varargs(1, 2, 3) # => (1, 2, 3)
# You can define functions that take a variable number of # 我们也可以定义一个关键字可变参数函数
# keyword arguments, as well
def keyword_args(**kwargs): def keyword_args(**kwargs):
return kwargs return kwargs
# Let's call it to see what happens # 我们来看看结果是什么:
keyword_args(big="foot", loch="ness") # => {"big": "foot", "loch": "ness"} keyword_args(big="foot", loch="ness") # => {"big": "foot", "loch": "ness"}
# You can do both at once, if you like # 这两种可变参数可以混着用
def all_the_args(*args, **kwargs): def all_the_args(*args, **kwargs):
print(args) print(args)
print(kwargs) print(kwargs)
@@ -433,8 +431,7 @@ all_the_args(1, 2, a=3, b=4) prints:
{"a": 3, "b": 4} {"a": 3, "b": 4}
""" """
# When calling functions, you can do the opposite of args/kwargs! # 调用可变参数函数时可以做跟上面相反的,用*展开序列,用**展开字典。
# Use * to expand tuples and use ** to expand kwargs.
args = (1, 2, 3, 4) args = (1, 2, 3, 4)
kwargs = {"a": 3, "b": 4} kwargs = {"a": 3, "b": 4}
all_the_args(*args) # equivalent to foo(1, 2, 3, 4) all_the_args(*args) # equivalent to foo(1, 2, 3, 4)
@@ -442,25 +439,25 @@ all_the_args(**kwargs) # equivalent to foo(a=3, b=4)
all_the_args(*args, **kwargs) # equivalent to foo(1, 2, 3, 4, a=3, b=4) all_the_args(*args, **kwargs) # equivalent to foo(1, 2, 3, 4, a=3, b=4)
# Function Scope # 函数作用域
x = 5 x = 5
def setX(num): def setX(num):
# Local var x not the same as global variable x # 局部作用域的x和全局域的x是不同的
x = num # => 43 x = num # => 43
print (x) # => 43 print (x) # => 43
def setGlobalX(num): def setGlobalX(num):
global x global x
print (x) # => 5 print (x) # => 5
x = num # global var x is now set to 6 x = num # 现在全局域的x被赋值
print (x) # => 6 print (x) # => 6
setX(43) setX(43)
setGlobalX(6) setGlobalX(6)
# Python has first class functions # 函数在Python是一等公民
def create_adder(x): def create_adder(x):
def adder(y): def adder(y):
return x + y return x + y
@@ -469,16 +466,14 @@ def create_adder(x):
add_10 = create_adder(10) add_10 = create_adder(10)
add_10(3) # => 13 add_10(3) # => 13
# There are also anonymous functions # 也有匿名函数
(lambda x: x > 2)(3) # => True (lambda x: x > 2)(3) # => True
# TODO - Fix for iterables # 内置的高阶函数
# There are built-in higher order functions
map(add_10, [1, 2, 3]) # => [11, 12, 13] map(add_10, [1, 2, 3]) # => [11, 12, 13]
filter(lambda x: x > 5, [3, 4, 5, 6, 7]) # => [6, 7] filter(lambda x: x > 5, [3, 4, 5, 6, 7]) # => [6, 7]
# We can use list comprehensions for nice maps and filters # 用列表推导式可以简化映射和过滤。列表推导式的返回值是另一个列表。
# List comprehension stores the output as a list which can itself be a nested list
[add_10(i) for i in [1, 2, 3]] # => [11, 12, 13] [add_10(i) for i in [1, 2, 3]] # => [11, 12, 13]
[x for x in [3, 4, 5, 6, 7] if x > 5] # => [6, 7] [x for x in [3, 4, 5, 6, 7] if x > 5] # => [6, 7]