>>> def foo():
... yield 5
... return 42
...
>>> foo()
<generator object foo at 0x7f01c5766f10>
>>> x = foo()
>>> next(x)
5
>>> next(x)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
StopIteration: 42
fun fact: if I have a program with 2 constants, they are almost invariably 5 and 42. 42 is self-evident as being my age less almost a decade; but the 5 I do not know where it comes from.
PSA to all API writers: please take the extra few hours so that if you offer different versions of an API, they all have the same functionality to the degree that makes sense.
@WayneWerner: I'll try not to make that mistake going forward.
TIL (because my daughters are crazy, or susceptible to mind control) that My Little Pony has something called the Friendship Games, which is a parody of the Hunger Games
I'm having a problem with this scenario: functionA assigns variable a=something. Inside functionA, functionB is called with no parameters. functionB creates a new variable b=a. I get a NameError because a was only defined within functionA. I had been hoping/thinking that nested functions keep the namespace of their "parents".
Do I really have to make 'a' a parameter of functionB in order to read (not write) its value?
@TomasZubiri: I don't mind it. It makes certain kinds of thing very convenient, and if you don't want it, you can always just not nest the function in the first place.
Then if it worked the way you wanted, @doublefelix, that sounds like a recipe for disaster. If you write a lot of small functions, you could have dozens of names bound along the call tree, none of which are easily inspected.
stackoverflow.com/q/39758921/674039 user uses enumerate to get an index while iterating a sequence, then uses it to index the sequence inside the iteration .. facepalm
Bwahaha turns out the bug I've been scratching my head about for two weeks is due to a bug that's been around for probably 10 years, and not recently broken by me. Hahaha. Ha. Ha. *curls up and cries*