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

  • SEARCH
  • Home
  • 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 7024143
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T23:48:07+00:00 2026-05-27T23:48:07+00:00

I have a code.py : def funA(): print(‘A’) funA() def funB(): print(‘B’) def funC():

  • 0

I have a code.py:

def funA():
    print('A')
    funA()

def funB():
    print('B')

def funC():
    print('C')
    funB()
    funC()

I want to find all functions call themself:

funA
funC

How to write the regex?

Constraint:

  • All function call is normal: funname(arg1, arg2, ...)
  • No obfuscated ways(such as lambda,exec)
  • No indirect recursion
  • 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-05-27T23:48:08+00:00Added an answer on May 27, 2026 at 11:48 pm

    Yes I do believe that it would be impossible for a regex to match the cases pointed out by wim where the self call is obfuscated. However, here is a regex that will do a semi decent job for straightforward self calls (in the form funcname(...)). This regex correctly matches all the test cases spelled out in the original question:

    reobj = re.compile(r"""
        # Match (unreliably) Python function with self reference.
        ^                        # Anchor to start of line.
        ([ \t]*)                 # $1: Indentation of DEF statement.
        def[ \t]+                # Function definition.
        ([^\s(]+)                # $2: Name of function to find.
        .*\r?\n                  # Remainder of function def line.
        (?:                      # Zero or more lines w/o funcname.
          (?:                    # Function block lines alternatives.
            \1[ \t]+             # Func block lines have extra indentation.
            (?:(?!\b\2\s*\().)*  # Optional non-funcname stuff on line
          | [ \t]*\#.*           # Allow comment lines to defy indent rules.
          )?                     # Allow blank lines in function block.
          \r?\n                  # End of line not containing funcname.
        )*                       # Zero or more lines w/o funcname
        \1[ \t]+                 # Now match the line having funcname.
        (?:(?!\b\2\s*\().)*      # Optional non-funcname stuff on line
        \b\2\s*\(                # Match the function self reference.
        """, re.MULTILINE | re.VERBOSE)
    

    It matches starting with the function definition line and captures the whitespace indentation before the 'def' in group $1 and the function name in group $2. It then matches lines within the function block that do not contain the function name which each have more leading whitespace than the function definition. It skips over empty lines and lines containing only comments. It declares a match once it finds a line in the function block which has the function name followed by a left parentheses, indicating a call to itself. Otherwise, it declares a non-match then continues looking for the next possible match.

    Note that this solution is unreliable and will result in false positives if the name of the function occurs inside a string or inside a comment following other code on a line. It also does not handle functions with multi-line raw strings. However, it will correctly catch quite a few!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have the following code def get(self): date = datetime.date.today() loc_query = Location.all() last_cursor
If I have a block of code like this: def show @post = Post.find(params[:id])
I have the following code: def foo(*args) print len(args) print args now I'd like
I have the following code: def testGeodatabase(self): geodatabaseList = self.gp.ListWorkspaces(*,ALL) for x in geodatabaseList:
I have some code like: def example(parameter): global str str = str(parameter) print(str) example(1)
I have the following code: def subStringMatchExact(target,key,matches=(),base=0): if find(target,key) != -1: matches += (find(target,key)+base,)
I have this code: def setVelocity (x, y, yaw) setVelocity (Command2d.new(x,y,yaw)) end def setVelocity
I have code: class Scene def initialize(number) @number = number end attr_reader :number end
I have code like this (simplified): def outer(): ctr = 0 def inner(): ctr
Let's say I have the following ruby code : def use_object(object) puts object.some_method end

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.