python - Variable set to call a function -
i hope question doesn't seem naive or silly, definite answer confirmation.
is possible set variable call function?
i.e
def func(): print "test" var = [something magical + func]
then if had typed/used var
output test
?
in other words, there alternate way calling function without use of parenthesis if never requires (or accepts) arguments [in python]?
i assuming isn't possible required use"()" call function, curious if there might exceptions?
in general, no. exception of properties mentioned in comments, need apply parameter list (even if it's empty ()
) call function. otherwise, you're not calling function, referencing function value.
you can reproduce on python's command line interpreter:
>>> def func(): ... return "foo" ... >>> func() # call function; statement returns function's return value 'foo' >>> func # reference function value; statement returns *the function itself* <function func @ 0x7f2c7a65b938> >>> var = func # assign function value variable >>> var # "var" references same function "func"... <function func @ 0x7f2c7a65b938> >>> var() # ...and can called function 'foo' >>>
Comments
Post a Comment