理解Python中的闭包范围
这是布拉特·斯莱特金书中的例子理解Python中的闭包范围,python,sorting,scope,closures,Python,Sorting,Scope,Closures,这是布拉特·斯莱特金书中的例子 def sort_priority(values, group): def helper(x): if x in group: return (0, x) return (1, x) values.sort(key=helper) 此外,他们给出了这些值 numbers = [8, 3, 1, 2, 5, 4, 7, 6] group = {2, 3,
def sort_priority(values, group):
def helper(x):
if x in group:
return (0, x)
return (1, x)
values.sort(key=helper)
此外,他们给出了这些值
numbers = [8, 3, 1, 2, 5, 4, 7, 6]
group = {2, 3, 5, 7}
sort_priority(numbers, group)
print(numbers)
我们有
[2, 3, 5, 7, 1, 4, 6, 8]
我不理解这个例子。为什么我们要返回两次,助手函数实际上做什么
为什么我们要返回两次
这与闭包或嵌套函数无关
def helper(x):
if x in group:
return (0, x)
return (1, x)
可以写成
def helper(x):
if x in group:
return (0, x)
else:
return (1, x)
无论哪种方式,返回值都取决于if
语句的计算结果。
如果为True
,则将返回(0,x)
。如果为False
,则将返回(1,x)
为什么我们要返回两次
这与闭包或嵌套函数无关
def helper(x):
if x in group:
return (0, x)
return (1, x)
可以写成
def helper(x):
if x in group:
return (0, x)
else:
return (1, x)
无论哪种方式,返回值都取决于if
语句的计算结果。
如果为
True
,则将返回(0,x)
。如果为False
,则返回(1,x)
。您将函数解读为:
def helper(x):
if x in group:
return (0, x)
else:
return (1, x)
或者更简单地说
def helper(x):
return (x not in group, x)
这背后的直觉是,sort
接受对每个元素调用的key
回调。对于每个元素,将调用helper
,它返回一个元组(可以是(0,x)
或(1,x)
,具体取决于VIP列表中是否存在x
)
您应该理解元组是基于多个谓词排序的,这意味着在决定元素的顺序时,元组中的两个项都会被考虑。这意味着组返回(0,x)
的元素将优先于返回(1,x)
的元素,因为0<1
在此之后,我们有两个组,第一个元素0
的组和第一个元素1
的组。所有0
组元素将排在第一位,但这些元素的顺序取决于元组中的第二项-x
。与1
组元素类似
请输入:
Group0: [2, 3, 5, 7]
Group1: [8, 1, 4, 6]
Ordering within Group0: [2, 3, 5, 7]
Ordering within Group1: [1, 4, 6, 8]
Overall ordering: [Group0, Group1]
Result: [2, 3, 5, 7, 1, 4, 6, 8]
您将函数解读为:
def helper(x):
if x in group:
return (0, x)
else:
return (1, x)
或者更简单地说
def helper(x):
return (x not in group, x)
这背后的直觉是,sort
接受对每个元素调用的key
回调。对于每个元素,将调用helper
,它返回一个元组(可以是(0,x)
或(1,x)
,具体取决于VIP列表中是否存在x
)
您应该理解元组是基于多个谓词排序的,这意味着在决定元素的顺序时,元组中的两个项都会被考虑。这意味着组返回(0,x)
的元素将优先于返回(1,x)
的元素,因为0<1
在此之后,我们有两个组,第一个元素0
的组和第一个元素1
的组。所有0
组元素将排在第一位,但这些元素的顺序取决于元组中的第二项-x
。与1
组元素类似
请输入:
Group0: [2, 3, 5, 7]
Group1: [8, 1, 4, 6]
Ordering within Group0: [2, 3, 5, 7]
Ordering within Group1: [1, 4, 6, 8]
Overall ordering: [Group0, Group1]
Result: [2, 3, 5, 7, 1, 4, 6, 8]
请注意,第一个
return
语句位于if
块中。在python中,每当函数遇到return语句时,就会将执行返回给调用方
在您的示例中,这两个返回只是避免if-else语句的快捷方式。当组中有特定值时,将返回
(0,x)
,如果条件不满足,则返回(1,x)
注意,第一个return
语句位于if
块中。在python中,每当函数遇到return语句时,就会将执行返回给调用方
在您的示例中,这两个返回只是避免if-else语句的快捷方式。当组中有特定值时,将返回(0,x)
,如果条件不满足,则返回(1,x)
如果编写的代码没有嵌套函数,那么理解代码就容易一些:
def helper(x):
global group
if x in group:
return 0, x
return 1, x
def sort_priority(values):
values.sort(key=helper)
numbers = [8, 3, 1, 2, 5, 4, 7, 6]
group = {2, 3, 5, 7}
sort_priority(numbers)
print(numbers)
现在很容易看出,sort\u priority()
只需调用helper
函数对值进行排序,该函数通过为每个x
分配一个值来创建顺序
当使用组中的值调用helper
函数时,它将获得“较低”的优先级(零),而如果该值不在组中,它将获得较高的优先级(一)
仔细查看helper
确实可以看出:
def helper(x):
global group
if x in group:
return 0, x # <-- we're in the `if` so `x` gets zero
return 1, x # <-- if we got here it means we didn't get into the `if` so `x` gets one
如果编写的代码没有嵌套函数,则更容易理解代码:
def helper(x):
global group
if x in group:
return 0, x
return 1, x
def sort_priority(values):
values.sort(key=helper)
numbers = [8, 3, 1, 2, 5, 4, 7, 6]
group = {2, 3, 5, 7}
sort_priority(numbers)
print(numbers)
现在很容易看出,sort\u priority()
只需调用helper
函数对值进行排序,该函数通过为每个x
分配一个值来创建顺序
当使用组中的值调用helper
函数时,它将获得“较低”的优先级(零),而如果该值不在组中,它将获得较高的优先级(一)
仔细查看helper
确实可以看出:
def helper(x):
global group
if x in group:
return 0, x # <-- we're in the `if` so `x` gets zero
return 1, x # <-- if we got here it means we didn't get into the `if` so `x` gets one
对我来说,更明显的是使用sorted(values)
函数而不是values.sort()
,否则“返回的是什么?”,“实际如何使用帮助程序?”,就有点模棱两可了
当然,在使用了sorted()
之后,sorted list显式地返回了它。对我来说更明显的是使用sorted(values)
函数而不是values.sort()
,否则“返回了什么?”,“实际如何使用helper?”,就有点含糊不清了
当然,在使用了sorted()
之后,sorted list会显式返回它。代码可以返回两个位置,但函数总是只返回一次!请说得更具体些,你不知道吗understand@alfasin看看我的编辑。代码可以返回两个地方,但函数总是只返回一次!Pl