Skip to content
Home » Functions Embedded in Data Structures

Functions Embedded in Data Structures

Spread the love

Today we’ll be talking about functions embedded in data structures.

Here’s the video version of this article:

In Python functions are described as first-class objects. This is because they are objects just like any other objects and can be used just like any other objects.

If you want to learn more about functions as first-class objects, you can read my article where I’m covering this topic in detail.

Anyway, as ordinary objects, functions can also be embedded in data structures like lists, tuples, dictionaries and so on.

We can use functions created by means of the def statement as well as lambdas. Let’s have a look at both cases. First, we’ll need a couple of functions, so let’s create some really basic ones we can then work on. The functions will be put in a list. Then we can iterate over the functions in a loop and call them.

def add(a, b):
    return a + b

def subtract(a, b):
    return a - b

def multiply(a, b):
    return a * b

def divide(a, b):
    return a / b

functions = [add, subtract, multiply, divide, lambda a, b: a ** b]

num1, num2 = 8, 5

for func in functions:
    # We use the __name__ attribute to retrieve the name of the function.
    print(f"Result of the {func.__name__} function with arguments {num1} and {num2}: {func(num1, num2)}")

The output:

Result of the add function with arguments 8 and 5: 13
Result of the subtract function with arguments 8 and 5: 3
Result of the multiply function with arguments 8 and 5: 40
Result of the divide function with arguments 8 and 5: 1.6
Result of the <lambda> function with arguments 8 and 5: 32768

Naturally,  we didn’t get the name of the last function, because it’s an anonymous function created by means of a lambda expression.

If you want to learn more about anonymous functions and lambda expressions, I have an article about them, so feel free to read it.

To be brief, lambdas are used to create functions, just like the def statement, but the functions don’t have a name (that’s why we call them anonymous) and they are usually quite simple.

In our example we embedded the functions in a list. Let’s have a look at an example with a dictionary. We’ll be using the same simple functions as before:

def add(a, b):
    return a + b

def subtract(a, b):
    return a - b

def multiply(a, b):
    return a * b

def divide(a, b):
    return a / b

functions = {'+': add, 
             '-': subtract, 
             '*': multiply, 
             '/': divide, 
             '^': lambda a, b: a ** b}

prompt = """Select an operation:

+ for adding 2 numbers
- for subtracting 2 numbers
* for multiplying 2 numbers
/ for dividing 2 numbers
^ for raising a number to a power

Your choice: """

operation = input(prompt)

numbers = (input("Enter 2 numbers, separating them with a comma: ")).split(',')

num1, num2 = float(numbers[0]), float(numbers[1])

func = functions[operation]
print(f"Result of the {func.__name__} function with arguments {num1} and {num2}: {func(num1, num2)}")

Here’s a possible output:

Select an operation:

+ for adding 2 numbers
- for subtracting 2 numbers
* for multiplying 2 numbers
/ for dividing 2 numbers
^ for raising a number to a power

Your choice: *
Enter 2 numbers, separating them with a comma: 2.4,10
Result of the multiply function with arguments 2.4 and 10.0: 24.0

Your Panda3D Magazine

Make Awesome Games and Other 3D Apps

with Panda3D and Blender using Python.

Cool stuff, easy to follow articles.

Get the magazine here (PDF).

Python Jumpstart Course

Learn the basics of Python, including OOP.

with lots of exercises, easy to follow

The course is available on Udemy.

Blender Jumpstart Course

Learn the basics of 3D modeling in Blender.

step-by-step, easy to follow, visually rich

The course is available on Udemy and on Skillshare.


Spread the love

Leave a Reply