|Programmer's Python - Function Objects|
|Written by Mike James|
|Monday, 07 January 2019|
Page 2 of 2
Functions are Objects
So far we have been looking at Python functions viewed as any function in any language might be described, but as promised, Python functions are completely different.
They are objects.
What does it mean that a function is an object?
When you write a Python function definition:
def sum(a,b): c=a+b return c
something more happens than in other languages.
When the Python system reads the function definition it does more than just store the name of the function in the dictionary. It creates a function object which stores the code of the function and sets a variable to reference it. The construction of an object to act as the function is key to the different way functions in Python work.
It is a good idea to think of a function definition more like:
sum = def(a,b): c=a+b return c
this is invalid syntax but if you think about it in this way you can see that the variable sum with a reference to the new function object is created.
The function object is a perfectly standard object but it comes with a code object as one of its attributes which stores the code. It is also an example of a callable which means you can use the invocation operator () to execute the code they contain.
Function objects have all of the built-in attributes that objects have and a few that are special and related to their callability.
is a variable that references the function object and:
is an evocation of that function object and it evaluates to whatever that function returns.
It may be only the difference of a pair of parentheses but the difference is huge.
If you are familiar with a more traditional approach to functions then it can take time to reach a point where you think about things correctly and stop making silly mistakes.
You can add new attributes to a function object and make use of them:
Notice that any attributes you create exist even when the function is not being evaluated, but local variables only exist while the function is being evaluated.
Function attributes and local variables have a different lifetime.
Why Are Functions Objects?
You may at this point be wondering why we bother to make functions objects?
After all it has already been stated that it is possible to write good Python code without giving the fact that functions are objects a moment’s thought.
What makes this approach so useful is often expressed by saying that in Python functions are first class objects.
What this means is that anything you can do with an object you can do with a function. In particular you can pass a function as a parameter to another function and you can return a function from another function.
These two simple features make things very much easier and we don’t have to invent additional mechanisms like delegates or lambdas to make them available.
The standard example is to consider a sort function which can accept a comparison function to use to order the things it is going to sort. A simpler, but less likely, example is a math function that can apply a function that you pass in:
def math(f,a,b): return f(a,b) sum.myAttribute=1 print(math(sum,1,2))
Notice that the first parameter f is a function. More accurately it is a function object and the math function evaluates it and returns its result.
Of course this doesn’t have any practical advantage unless you are going to have a range of possible functions that can be passed as f but you can see how this might work in practice. Being able to customize one function by passing it others to use is a huge simplification and there are other advantages of function objects.
|Last Updated ( Monday, 07 January 2019 )|