As already mentioned, functions and methods are first-class objects. You call them by casting brackets (parentheses) at the end. But it looks like you need another motivation for why python even allows us to do this. Why should we be wondering if features are first-class or not?
Sometimes you donโt want to call them, you want to pass a link to the called yourself.
from multiprocessing import Process t = Process(target=my_long_running_function)
If you put the brackets after the above, it runs your my_long_running_function in your main thread; hardly what you wanted! You would like to give Process link to your called code so that it starts in a new process.
Sometimes you just want to specify the callee and allow something else ...
def do_something(s): return s[::-1].upper() map(do_something,['hey','what up','yo']) Out[3]: ['YEH', 'PU TAHW', 'OY']
( map in this case) fill in your arguments.
Maybe you just want to drop a bunch of calls into some collection and pull out the one you want dynamically.
from operator import * str_ops = {'<':lt,'>':gt,'==':eq} # etc op = str_ops.get(my_operator) if op: result = op(lhs,rhs)
The above method is to match the string representations of the operators with their actual action.
roippi
source share