Why does the assignment operation print? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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?

9th May 2021, 1:04 PM
Tim
3 Answers
+ 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.
9th May 2021, 4:14 PM
Ipang
+ 1
Aha, so the () part means the function call. Thanks everyone for your replies.
11th May 2021, 5:23 PM
Tim
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.
9th May 2021, 1:14 PM
你知道規則,我也是
你知道規則,我也是 - avatar