Fundamentals 6 min read

10 Common Ways to Call Functions in Python

This article introduces ten frequently used Python function‑calling techniques—including built‑ins like filter, eval, exec and utilities from functools and operator—explaining their purpose, typical use cases, and providing clear code examples for each method.

Python Programming Learning Circle
Python Programming Learning Circle
Python Programming Learning Circle
10 Common Ways to Call Functions in Python

Python offers a variety of built‑in functions and utilities that can be used to invoke or manipulate functions in different programming scenarios. Below are ten commonly used approaches, each illustrated with a short code snippet.

1. filter() – Filters elements of an iterable based on a predicate function.

python
numbers = [1, 2, 3, 4, 5]
odd = filter(lambda x: x % 2, numbers)
print(list(odd))  # [1, 3, 5]

2. getattr() – Retrieves an attribute or method from an object using its name as a string.

python
class MyClass:
    def my_method(self):
        print("Hello, World!")

obj = MyClass()
method = getattr(obj, "my_method")
method()  # Hello, World!

3. partial() – From functools , creates a new callable with some arguments pre‑filled.

python
from functools import partial

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

add_five = partial(add, 5)
print(add_five(3))  # 8

4. eval() – Executes a string containing a Python expression and returns its result.

python
expr = "2 + 3"
result = eval(expr)
print(result)  # 5

5. dict (object.__dict__) – Provides a dictionary of an object’s writable attributes.

python
class MyClass:
    def __init__(self):
        self.x = 5

obj = MyClass()
print(obj.__dict__)  # {'x': 5}

6. globals() – Returns a dictionary representing the current global symbol table.

python
x = 10
y = 20
print(globals())  # {..., 'x': 10, 'y': 20, ...}

7. exec() – Executes dynamically generated Python code.

python
code = """
def multiply(a, b):
    return a * b

print(multiply(3, 5))
"""
exec(code)  # 15

8. attrgetter() – From operator , creates a callable that fetches a given attribute from its operand.

python
from operator import attrgetter

class Person:
    def __init__(self, name):
        self.name = name

people = [Person('Alice'), Person('Bob')]
get_name = attrgetter('name')
print([get_name(p) for p in people])  # ['Alice', 'Bob']

9. methodcaller() – From operator , returns a callable that calls a named method on its operand.

python
from operator import methodcaller

class Greeter:
    def say(self):
        print('Hello')

g = Greeter()
call_say = methodcaller('say')
call_say(g)  # Hello

10. call (object.__call__) – Defines how an instance behaves when called like a function.

python
class Adder:
    def __call__(self, a, b):
        return a + b

adder = Adder()
print(adder(3, 5))  # 8

These techniques serve different purposes—some are useful for functional or meta‑programming, while others simplify repetitive tasks or dynamic code execution. Selecting the appropriate method depends on the specific requirements of your Python project.

PythonOperatorcode examplesFunctionsfunctoolsbuiltin
Python Programming Learning Circle
Written by

Python Programming Learning Circle

A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.