Objects in Python — Learn to Use Functions as Objects
Introduction:
You’ve probably heard the saying that everything is an object in Python — one of the most popular object-oriented languages. When we talk about objects, we usually refer an object to a collection of data, commonly known as attributes, and the object has certain predefined functions to update these data or exchange data with other objects.
Python id:
The id() function returns identity (unique integer) of an object.
The syntax of id() is:
id() Parameters
id() function takes a single parameter object.
Return Value from id()
id() function returns the identity of the object. This is an integer that is unique for the given object and remains constant during its lifetime.
More Examples on id()
Data Types as Objects
All data types, such as numbers, strings, lists, and dictionaries, are all objects in Python. Using a tangible example, you can think about them physically as boxes of data. In terms of size, some boxes are smaller by just holding an int, a bool, or a str, while some boxes are larger by holding a list or a dict. The difference is their size reflects the needed memory for storing these data in the computer. Certainly, boxes can have other characteristics, such as color and material, for which you can conceptualize them as different types of data associated with each box.
Being an object, the following common implications or functionalities apply.
- It can be assigned to a variable.
- It can be an argument passed to a function.
- It can be a returned value from a function.
- It can be associated with other objects as part of their data.
# Assign an int to a variable a = 5 # Use an int as an argument in a function def add_two(a): return a + 2 add_two(6) # Return a string in a function def is_even(a): return 'even' if a % 2 == 0 else 'odd' is_even(745) # A boolean value in a tuple (True, 7, 8)
In the above code snippet, you see examples of these implications for Python objects, like int and string. Even if you don’t know too much about Python coding or any programming languages, they are not too hard to understand, right?
However, it might be a little puzzling to some people when someone says that functions are also objects in Python. Why can he/she say that? Let’s check it out in the next section.
Functions as Objects
Before we start the discussion about functions as objects, let’s review what a function is — a named block of code that takes zero or more inputs and returns an output after running certain computation. Here’s a simple example that illustrates the key components as highlighted in the above definition.
# define a function named add_two # the function takes an input int as its argument def add_two(a): # the function is to add 2 to the input argument b = a + 2 # the function returns the sum as output return b
In the above section, we mentioned that there are some common functionalities that a Python data object has. I will show you how a function provides such functionalities with some examples.
Variable Assignment
In the code snippet below, I created a function called greeting(), and assigned it to the variable say_hello, which produced the same kind of output as we called greeting().
One thing to note that both greeting() and say_hello() refers to the same object in memory. For example, if you check their addresses, they have the same value as shown below. If we delete the function greeting(), the name “greeting” will become undefined, while the deletion doesn’t affect the underlying object, which is referenced by the “say_hello” function. Thus, an important concept here is that the variable referring to the function is different from the actual object saved in the memory.
Use Function as Another Function’s Argument
Since a function is an object, we can pass a function as another function’s argument. Shown below, I created three functions: combine_two_numbers(), add_two_numbers(), and multiply_two_numbers(), the latter of which are to compute the sum and product of two numbers in a tuple. Something different from what you’ve seen mostly is the first function combine_two_numbers(), which takes a function and a tuple. As you can see in the examples, we pass the add_two_numbers and multiply_two_numbers as the argument, which further calculates the sum and the product of the tuple of the numbers, respectively.
Function as Return Value
Another interesting feature with Python’s functions being objects is that it can be a valid return value of a function. Let’s see an illustration below. Specifically, I wrote a function called add_number_creator(), which returns a function that can add a number to an integer argument. Two functions, add_three() and add_five(), were created by calling this add_number_creator() function, and these two functions do what they’re expected to do by adding 3 and 5 to the integers specified in the function call.
Function as Part of Another Object
As a function is an object, we can create a list of functions, such that the functions become part of the list object. A simple example is shown below. Following up on the last example, we created three functions that add 0, 1, and 2 to an input integer by using the add_number_creator() function. As you can see, we can enumerate the list of functions as we normally do with a list of integers or strings.
Conclusions
This article shows you that functions are just like other objects in Python, and they can be assigned to variables and used in functions. Certainly, simple examples are used above for demonstration purposes, and the idea is the same that if you encounter more complex situations, you can apply the same principles to use your functions as regular objects in your Python coding.