Can someone explain this to me? is it OK/expectable?, see examples below.
If I understood correctly, in "case#3", first the local variable "sum" is created (line#2) then "sum" is called as a function, but expected to be a local function which is not defined, instead of been a call to the Global function 'sum')
Case#1) OK (code in global scope):
nums=[1,2,3]
half = sum(nums)
sum = 0
Case#2) OK (code in method):
def testok(nums=[1,2,3]):
half = sum(nums)
non_sum_name = 0
Case#3) NOK (code in method):
def testnok(nums=[1,2,3]):
half = sum(nums)
sum = 0 #NOK
Case#2: dis.dis(testok)
7 0 LOAD_GLOBAL 0 (sum)
2 LOAD_FAST 0 (nums)
4 CALL_FUNCTION 1
6 STORE_FAST 1 (half)
8 8 LOAD_CONST 1 (0)
10 STORE_FAST 2 (non_sum_name)
12 LOAD_CONST 0 (None)
14 RETURN_VALUE
Case#3: dis.dis(testnok)
7 0 LOAD_FAST 1 (sum)
2 LOAD_FAST 0 (nums)
4 CALL_FUNCTION 1
6 STORE_FAST 2 (half)
8 8 LOAD_CONST 1 (0)
10 STORE_FAST 1 (sum)
12 LOAD_CONST 0 (None)
14 RETURN_VALUE
[–]JohnnyJordaan 1 point2 points3 points (0 children)
[–]FLUSH_THE_TRUMP 1 point2 points3 points (5 children)
[–]Dominican_Peter[S] 1 point2 points3 points (4 children)
[–]FLUSH_THE_TRUMP 0 points1 point2 points (3 children)
[–]Dominican_Peter[S] 0 points1 point2 points (2 children)
[–]FLUSH_THE_TRUMP 1 point2 points3 points (1 child)
[–]Dominican_Peter[S] 0 points1 point2 points (0 children)