0
what does an assignment really do?
I thought that an assignment like var = some_func () would just associate the new name var to the already existing value of the function some_func, but why do the following two last print lines give me 2 different outputs? Since var is assigned to be the same as some_func, why don't they both return None as output? def some_func(): print("Hi!") var = some_func() print(var) print(some_func())
3 odpowiedzi
+ 3
And you can also do:
var = some_func
print(var())
but in theses exemple, print() ( outside some_func() ) is not necessary... In reality, you execute the some_function() which return anything, just do print("Hi!"). So the print(some_func()) ( and the equivalents ) just print anything, after the some_function() already print: "Hi!"... So we can rewrite:
def some_func():
print("Hi!")
var = some_func() # at here, var is always empty and "Hi!" is already printed ^^
print(var) # so, nothing is printed
print(some_func()) # the some_func() call print "Hi!" a second time, and the outter print nothing
some_func() # and this visually do the same, but shorter
var = some_func # now, var contain the function itself
var() # so we can call the some_func() by calling var() and now "Hi!" is printed again
But also:
def some_func():
return "Hi!"
var = some_func() # at here, var is containing "Hi!" and nothing is printed ^^
print(var) # so, print "Hi!" a first time
print(some_func()) # the some_func() call return "Hi!", and the outter print it a second time
some_func() # but just call some_function print nothing ( return "Hi!", but it's lost because we don't store it )
var = some_func # now, var contain the function itself
var() # so we can call the some_func() by calling var() but without printing ( "Hi!" is returned, but not stored )
+ 2
var is associated to function call.
In the last line use
print(some_func())
0
Try this
def f:
print("hi")
print(f)
myPointerToF = f
print(myPointerToF)