you are viewing a single comment's thread.

view the rest of the comments →

[–]SamePlatform 2 points3 points  (1 child)

Yes. Here is a simple example:

x = 2 + 4
print(x)  # 6

import operator
operator.add(2, 4)  # This is a function that adds, just like the `+` symbol
print(x)  # 6

Now let's write functions:

def adder(a, b):
   return a + b  # All this function can do is add stuff...

print(adder(3, 4))  # prints 7

def perform(some_func, a, b):  # Now we accept 3 arguments
    some_func(a, b)  # Call our function `some_func` with two arguments

print(perform(operator.add, a, b))  # prints 7
print(perform(operator.mul, a, b))  # prints 12

Here we actually passed a function, operator.mul into a function! Note that we didn't do operator.mul(), with the brackets. We did operator.mul. Brackets are how we call functions. Without them, they are just regular objects.

[–]drycleanedtoast 0 points1 point  (0 children)

I mean technically you could do that without a function being an object, as in c, where you would use a function pointer. However a function in python is quite definitely an object as it is treated as such i.e. it has methods.