Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8398695
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T21:06:55+00:00 2026-06-09T21:06:55+00:00

How can I determine programmatically if one function calls another function? I cannot modify

  • 0

How can I determine programmatically if one function calls another function? I cannot modify either function.

Here’s what I want (source_calls_target):

>>> def check():
>>>     pass
>>> def test_check():
>>>     check()
>>> def source_calls_target(source, target):
>>>     # if source() calls target() somewhere, return True
>>>     ???
>>> source_calls_target(test_check, check)
True
>>> source_calls_target(check, test_check)
False

Ideally, I do not want to actually call target().

Ideally, I want to check if a call to target() appears within the definition for source. It may or may not actually call it depending on conditional statements.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T21:06:57+00:00Added an answer on June 9, 2026 at 9:06 pm

    If you can guarantee having access to the source code, you can use ast.parse:

    import ast
    call_names = [c.func.id for c in ast.walk(ast.parse(inspect.getsource(source)))
                  if isinstance(c, ast.Call)]
    return 'target' in call_names
    

    Note that calls are always by name, so it’s difficult (and potentially impossible) to tell whether a call is to a particular function or another of the same name.

    In the absence of source code, the only way is via disassembly:

    import dis
    def ops(code):
        i, n = 0, len(code)
        while i < n:
            op = ord(code[i])
            i += 1
            if op == dis.EXTENDED_ARG:
                ext = ord(code[i]) + ord(code[i+1])*256
                op = ord(code[i + 2])
                i += 3
            else:
                ext = 0
            if op >= dis.HAVE_ARGUMENT:
                arg = ord(code[i]) + ord(code[i+1])*256 + ext*65536
                i += 2
                yield op, arg
            else:
                yield op, None
    
    source_ops = list(ops(source.func_code.co_code))
    

    The problem is that it’s in practice impossible to tell whether a function is calling another function or just loading a reference to it; if the other function is passed to map or reduce etc. then it will be called but passed to another function it might not be. Practically the sensible thing is to assume that if the function is in source.func_code.co_names then it might be called:

    'target' in source.func_code.co_names
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can one programmatically determine logins/users that have permission to access specific SSRS reports?
How can I determine programmatically whether my machine is an x86, x64 or an
I know you can programmatically determine the tranparency color of a .gif Is there
How can I programmatically determine sites of a SharePoint 2007 installation that use custom
In Windows, how can I programmatically determine which user account last changed or deleted
How can I programmatically determine which (if any) mounted volumes are a DVD? I
How can I determine programmatically what master pages (custom and OOTB) that are available
How can I check programmatically, how much memory my program currently has allocated ?
Similar to this question but for MySQL.... How can I programmatically determine foreign key
In C/CUDA, if I am passed a pointer, how can I programmatically determine whether

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.