DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • From Indicators to Insights: Automating IOC Enrichment Using Python and Threat Feeds
  • Stop Poisoning Your Models: How I Built a CV Dataset Quality Toolkit I Can Reuse Forever
  • DuckDB for Python Developers
  • Building a Production-Ready AI Agent in 2026: Beyond the Hello World Demo

Trending

  • Context-Aware Authorization for AI Agents
  • How Rule Engines Transform Business Agility and Code Simplicity
  • The Vector Database Lie
  • The Prompt Isn't Hiding Inside the Image
  1. DZone
  2. Coding
  3. Languages
  4. Python — How to Tell if a Function Has Been Called

Python — How to Tell if a Function Has Been Called

For the Raspberry Pi devs: how to know for sure if a function has been called in your Python code.

By 
Mike Driscoll user avatar
Mike Driscoll
·
Mar. 14, 17 · Tutorial
Likes (8)
Comment
Save
Tweet
Share
24.4K Views

Join the DZone community and get the full member experience.

Join For Free

Last year I ran into a situation where I needed to know if a function had been called. Basically we were trying to prevent shutting down a Twisted event loop twice or starting two of them. Anyway, in my research I stumbled across a fun post on StackOverflow that showed a couple of ways to do this.

The first made use of the fact that everything in Python is an object, including the function itself. Let’s take a look at a simple example:

def self_aware_function(a, b):
    self_aware_function.has_been_called = True
    return a + b

if __name__ == '__main__':
    self_aware_function.has_been_called = False

    for i in range(2):
        if self_aware_function.has_been_called:
            print 'function already called'
        else:
            print 'function not called'

        self_aware_function(1, 2)

In this example, we create an attribute on the function that we named has_been_called. We set it to True when the function is called. When you start your program, you will want to initialize that attribute to False, which we do above. Then we use a for loop to loop twice. The first time through it will check if the function has been called. Since it hasn’t, you will see it fall to the else statement. Now that we called the function, the second time through the loop the first part of the if statement executes.

That StackOverflow post also mentions a neat way to use a decorator to track function calls. Here’s an example I wrote:

import functools


def calltracker(func):
    @functools.wraps(func)
    def wrapper(*args):
        wrapper.has_been_called = True
        return func(*args)
    wrapper.has_been_called = False
    return wrapper

@calltracker
def doubler(number):
    return number * 2

if __name__ == '__main__':
    if not doubler.has_been_called:
        print "You haven't called this function yet"
        doubler(2)

    if doubler.has_been_called:
        print 'doubler has been called!'

In this example, I import functools and create a decorator that I dubbed calltracker. In this function, we set up the same attribute that we did in the previous example, but in this case we attach it to our wrapper (i.e. the decorator). Then we decorate a function and give our code a try. The first if statement checks to see if the function has been called yet. It hasn’t, so we go ahead and call it. Then we confirm that the function was called in our second if statement.

Wrapping Up

While this stuff is certainly useful at runtime, you can also do similar things using Python’s trace module to trace through the execution of your code. This sort of thing is also done via coverage tools. You will also find this kind of functionality in Python Mock objects as a Mock can tell when it has been called.

Anyway, you will hopefully find this exercise as interesting as I did. While I already knew that everything in Python was an object, I hadn’t thought about using that functionality to add attributes to functions.

Python (language)

Published at DZone with permission of Mike Driscoll. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • From Indicators to Insights: Automating IOC Enrichment Using Python and Threat Feeds
  • Stop Poisoning Your Models: How I Built a CV Dataset Quality Toolkit I Can Reuse Forever
  • DuckDB for Python Developers
  • Building a Production-Ready AI Agent in 2026: Beyond the Hello World Demo

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook