0
Why does the assignment operation print?
I have the following code: def some_func(): print("Hi!") var = some_func() When I run this code I get the following output: Hi! I don't understand why "Hi!" gets printed. I don't run the function, I only assign it to the variable. Could someone please explain?
3 Respostas
+ 1
Try to assign only function name
var = some_func # note there's no ()
Later on you can use <var> as you would some_func function.
var() # calls some_func function
The () after function name means to invoke the function. It is not needed to assign a function for a variable.
+ 1
Aha, so the () part means the function call. Thanks everyone for your replies.
0
Before you assign the value, the function has to be executed.
For example a function:
def func(i):
i += 50
return i
x = func(30)
func(30) should execute 30+50 before assign it to x.