問題描述
我遇到了 Jake VanderPlas 提出的這個問題,我不確定我對導(dǎo)入 numpy 模塊后結(jié)果為何不同的理解是否完全正確.
I came across this problem by Jake VanderPlas and I am not sure if my understanding of why the result differs after importing the numpy module is entirely correct.
>>print(sum(range(5),-1)
>> 9
>> from numpy import *
>> print(sum(range(5),-1))
>> 10
似乎在第一種情況下,sum 函數(shù)計算迭代的總和,然后從總和中減去第二個 args 值.
It seems like in the first scenario the sum function calculates the sum over the iterable and then subtracts the second args value from the sum.
在第二種情況下,在導(dǎo)入 numpy 后,函數(shù)的行為似乎發(fā)生了變化,因為第二個 arg 用于指定執(zhí)行求和的軸.
In the second scenario, after importing numpy, the behavior of the function seems to have modified as the second arg is used to specify the axis along which the sum should be performed.
練習(xí)編號 (24)來源 - http://www.labri.fr/perso/nrougier/teaching/numpy.100/index.html
Exercise number (24) Source - http://www.labri.fr/perso/nrougier/teaching/numpy.100/index.html
推薦答案
只添加我的5個迂腐幣到@Warren Weckesser 回答.真的 from numpy import *
不會覆蓋 builtins
sum
函數(shù),它只是陰影 __builtins__.sum
,因為 from ... import *
語句將導(dǎo)入模塊中定義的所有名稱(以下劃線開頭的名稱除外)綁定到您當(dāng)前的 global
命名空間.并且根據(jù) Python 的名稱解析規(guī)則(非官方 LEGB 規(guī)則),global
命名空間在 __builtins__
命名空間之前查找.因此,如果 Python 找到所需的名稱,在您的情況下為 sum
,它會返回綁定的對象并且不會進(jìn)一步查找.
Only to add my 5 pedantic coins to @Warren Weckesser answer. Really from numpy import *
does not overwrite the builtins
sum
function, it only shadows __builtins__.sum
, because from ... import *
statement binds all names defined in the imported module, except those beginning with an underscore, to your current global
namespace. And according to Python's name resolution rule (unofficialy LEGB rule), the global
namespace is looked up before __builtins__
namespace. So if Python finds desired name, in your case sum
, it returns you the binded object and does not look further.
編輯:向您展示發(fā)生了什么:
EDIT: To show you what is going on:
In[1]: print(sum, ' from ', sum.__module__) # here you see the standard `sum` function
Out[1]: <built-in function sum> from builtins
In[2]: from numpy import * # from here it is shadowed
print(sum, ' from ', sum.__module__)
Out[2]: <function sum at 0x00000229B30E2730> from numpy.core.fromnumeric
In[3]: del sum # here you restore things back
print(sum, ' from ', sum.__module__)
Out[3]: <built-in function sum> from builtins
第一個說明:del
不會刪除對象,它是垃圾收集器的任務(wù),它只是取消引用"名稱綁定并從當(dāng)前命名空間中刪除名稱.
First note: del
does not delete objects, it is a task of garbage collector, it only "dereference" the name-bindings and delete names from current namespace.
第二個說明:內(nèi)置sum
函數(shù)的簽名是sum(iterable[, start])
:
將 start
和 iterable
的項目從左到右求和并返回總數(shù).start
默認(rèn)為 0
.iterable的item一般是數(shù)字,起始值不允許是字符串.
Sums
start
and the items of aniterable
from left to right and returns the total.start
defaults to0
. The iterable‘s items are normally numbers, and the start value is not allowed to be a string.
我你的情況 print(sum(range(5),-1)
用于內(nèi)置 sum
總和以 -1 開頭.所以從技術(shù)上講,你的短語 iterable 的總和,然后從總和中減去第二個 args 值 是不正確的.對于數(shù)字,以 或 add/subtract<開始并不重要/em> 稍后.但是對于列表它確實如此(愚蠢的例子只是為了展示這個想法):
I your case print(sum(range(5),-1)
for built-in sum
summation starts with -1. So technically, your phrase the sum over the iterable and then subtracts the second args value from the sum isn't correct. For numbers it's really does not matter to start with or add/subtract later. But for lists it does (silly example only to show the idea):
In[1]: sum([[1], [2], [3]], [4])
Out[1]: [4, 1, 2, 3] # not [1, 2, 3, 4]
希望這能澄清你的想法:)
Hope this will clarify your thoughts :)
這篇關(guān)于python sum() 導(dǎo)入numpy后結(jié)果不同的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!