+ 32

Early Global Object Destruction in Functions?

So I was inspecting a Python code: x = input().split(',') y = map(int,input().split(',')) def n(): return sum(y) def m(): return sum(y)/len(x) print(n()) print(m()) with the input: a,b,c 1,2,3 I realized that for some reason, the second print which calls m() would always return 0.0. Surprisingly, removing the first print from the code will cause m() to properly return 2.0. Probing deeper, I found out that sum(y) in m() returned 0.0. The map object stored within y appears to have been destroyed after the first function call (n()). Replacing the map object with a list: y = list(map(int,input().split(','))) would fix the issue, but how is this possible? Thread which contains the original code which caught my attention: https://www.sololearn.com/Discuss/1347558/?ref=app

15th Jun 2018, 11:06 AM
Hatsy Rei
Hatsy Rei - avatar
8 Answers
+ 21
The <map object> you are seeing is a generator. So you can imagine it like a for loop with a couple of `yield` statements in it, like def map(fn, xs): for x in xs: yield fn(x) (I don't know if that's how it's implemented but it does the same*) Once you've run through the input array once you're done, and you'd need to call map again. Or use `list`, to collect all the `yield`ed values first! __ *Looked it up, cpython implements it in c, code looks about right. https://github.com/JUMP_LINK__&&__python__&&__JUMP_LINK/cpython/blob/master/Python/bltinmodule.c line 1280 PyPy returns a list (see KrOW s answer.) https://github.com/mozillazg/pypy/blob/master/pypy/module/__builtin__/app_functional.py line 83
15th Jun 2018, 11:16 AM
Schindlabua
Schindlabua - avatar
+ 15
Schindlabua Oh, so that's why. The map() function returns a generator iterator, and such objects can only be iterated over once... The more you know. Thanks!
15th Jun 2018, 11:48 AM
Hatsy Rei
Hatsy Rei - avatar
+ 8
Yeah I agree with Schindlabua. Yes that's how the map object sorta works like.
15th Jun 2018, 11:30 AM
👑 Prometheus 🇸🇬
👑 Prometheus 🇸🇬 - avatar
+ 6
I want precise that this apply only to python 3.x... On previous map dont return an iterator but a list then all can works like expected
15th Jun 2018, 11:59 AM
KrOW
KrOW - avatar
+ 4
Thanks for the explanation of map objects as generators. All the other languages that I have used, map returns a list of applying a function to all the elements of map's second input. I would of fallen into the same trap
18th Jun 2018, 11:08 PM
Rick Shiffman
Rick Shiffman - avatar
+ 2
Hi Raman Saini, That's a holdover from the UNIX operating system or Linux. If a program succeeded it would return zero if it had an error it would return a nonzero number which was the error code. The shell could look at this code and decide what to do.
30th Jun 2018, 12:17 AM
Rick Shiffman
Rick Shiffman - avatar
+ 1
i am a begginer is anyone tell me that why we use return 0; in some int main() and in some cases we dont use that
29th Jun 2018, 11:59 AM
Raman
Raman - avatar
0
Raman Saini You can explain why you dont have opened another question or better used the search feature?
30th Jun 2018, 6:10 AM
KrOW
KrOW - avatar