+ 2
Calling a function in Python
Lets say I have a function: def add(i): total = 0 total = total + i return total thingy = add(1) If after the function definition, I wrote 'add(2) ' I understand that I will get back 2 and this is shown on the console log. But I don't understand 'thingy=add(1)'??? Does my program actually calls it and displays the result on the console log? Or does it compute add(1) and just stores the resulting value 1 into the variable 'thingy?' Also, if inside the function definition, I just wrote 'return'...will I see on the console log 'none'? I ask this as I am trying to learn generators and some examples are confusing. Any help is greatly appreciated.
8 Respostas
+ 6
Calling a function in Python
Lets say I have a function:
def add(i):
total = 0
total = total + i
return total
thingy = add(1)
Here thingy is a variable which stores the result..
If u want to print the result u can use..
Print(thingy) # as the last line of ur program..
Return statement returns the total value to the function call which is made to store in thingy....
+ 1
return just returns a value from the function, it doesn't produce any output.
If you write, 'add(2)', you will see no result.
Easiest is to imagine, that the function call gets substituted against its return value.
def f():
return 42
f() then is the same as 42.
x = f() means x = 42
print(f()) means print(42).
+ 1
What you are looking for is the yield and next keywords
You "cannot" create a generator without the yield keyword and you cannot get values from a generator without using next or for
example
https://code.sololearn.com/ca9bJaC2g8qA/?ref=app
0
J3fro C add is a function and thus it returns a value meaning that in most cases everytime you call add e.g. add(1) a distinct value is returned
0
@Paul
But I thought with regards to 'yield' it would just pause the function and not exit the function like what 'return' would do.
So I expected when I typed:
print(next(add(1)))
print(next(add(1)))
It will continue where it paused and just go through the while loop again and hence, give me 2?
Going by what you recently said, it means that the function would Completely start over again with the argument 1?
I just don't understand why you needed to place the resulting value of 'add(1)' in to a variable called my_nums
Can I just confirm that with the line:
gen1 = add(1)
The value of gen1 is initially 1 but the function ''add'' goes infinitely due to the while loop, thus gen1's value is a sequence of numbers i.e 1,2,3,4,5,6,7,....?
So in a sense, gen1 value would constantly be going up by 1 if it weren't for the 'yield' keyword? Furthermore, add(1) is NOT a list?
0
gen1 does not have a value, gen1 is a generator.
That's the hard part to understand with generator. As soon as you use yield in a function, this function returns a generator everytime it is called and not a value or a serie of values
0
I think with abit more searching I understand it. Just want to confirm it on here. For the code Paul has given:
When 'next()' is applied to 'gen1', the 'next()' function request off 'gen1' a value. 'gen1' will call 'add(i)' and 'add(i)' will perform it's instructional code till the 'yield' is come across. It will give the yield value to the generator object. Then 'gen1' will pause the generating function and remember's the state.
So even though when it says: gen1 = add(1)
gen1 doesn't have a value intially. It will only yield a value (to which it gives it to next()) ONLY when the 'next()' function is applied onto it.
Hope this is accurate?
Note to myself: generator object is like a saving point in a game. Remembers it's latest state and the current value it's on. When next() is applied to it, the object calls for a new yield value via un-pausing the function. Next() then retrieves the value the object is currently on. Then repeats whats mentioned in the second sentence.
0
In the code I linked, calling add(1) does not run any code within the add function. This is the hard part to understand with yield. To make it easier to understand I have added print statements to my example to help you