Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detecting empty function definitions in python

I need to detect whether a function is an empty definition or not. It can be like:

def foo():
    pass

or like:

def foo(i, *arg, **kwargs):
    pass

or like:

foo = lambda x: None

What is the most elegant way to detect them using the 'inspect' module? Is there a better way than this:

def isEmptyFunction(func):
    e = lambda: None
    return func.__code__.co_code == e.__code__.co_code
like image 738
Benji Mizrahi Avatar asked Nov 29 '12 07:11

Benji Mizrahi


People also ask

How do you check if a function is empty in Python?

Solution 3: Using len() In this solution, we use the len() to check if a list is empty, this function returns the length of the argument passed. And given the length of an empty list is 0 it can be used to check if a list is empty in Python.

How do you check if a function is empty?

The empty() function checks whether a variable is empty or not. This function returns false if the variable exists and is not empty, otherwise it returns true. The following values evaluates to empty: 0.

What is empty function in Python?

An empty function is a function that does not contain any statement within its body. If you try to write a function definition without any statement in python – it will return an error ("IndentationError: expected an indented block").

What is an empty function called?

"No-op" (for "no operation", commonly shortened to "noop" as elclanrs mentions) or "empty" are commonly understood (in any language with functions).


2 Answers

To answer the original question: I don't think there is a better way, but definitely a more resilient one.

Building on top of this answer by @kcon:

def isEmptyFunction(func): 
    def empty_func(): 
        pass

    def empty_func_with_doc(): 
        """Empty function with docstring.""" 
        pass 

    return func.__code__.co_code == empty_func.__code__.co_code or \
        func.__code__.co_code == empty_func_with_doc.__code__.co_code

which fails for the following:

def not_empty_returning_string():
    return 'not empty'

isEmptyFunction(just_return_string) # True

as well as for lambdas:

not_empty_lambda_returning_string = lambda x: 'not empty'

isEmptyFunction(not_empty_lambda_returning_string) # True

I built an extended version which also checks constants with the exception of docstrings:

def is_empty_function(f):
    """Returns true if f is an empty function."""

    def empty_func():
        pass

    def empty_func_with_docstring():
        """Empty function with docstring."""
        pass

    empty_lambda = lambda: None

    empty_lambda_with_docstring = lambda: None
    empty_lambda_with_docstring.__doc__ = """Empty function with docstring."""

    def constants(f):
        """Return a tuple containing all the constants of a function without:
            * docstring
        """
        return tuple(
            x
            for x in f.__code__.co_consts
            if x != f.__doc__
        )

    return (
            f.__code__.co_code == empty_func.__code__.co_code and
            constants(f) == constants(empty_func)
        ) or (
            f.__code__.co_code == empty_func_with_docstring.__code__.co_code and
            constants(f) == constants(empty_func_with_docstring)
        ) or (
            f.__code__.co_code == empty_lambda.__code__.co_code and
            constants(f) == constants(empty_lambda)
        ) or (
            f.__code__.co_code == empty_lambda_with_docstring.__code__.co_code and
            constants(f) == constants(empty_lambda_with_docstring)
        )

Testing:

#
# Empty functions (expect: is_empty_function(f) == True)
#

def empty():
    pass

def empty_with_docstring():
    """this is just an example docstring."""
    pass

empty_lambda = lambda: None

empty_lambda_with_docstring = lambda: None
empty_lambda_with_docstring.__doc__ = """this is just an example docstring."""

#
# Not empty functions (expect: is_empty_function(f) == False)
#

def not_empty():
    print('not empty');

def not_empty_with_docstring():
    """this is just an example docstring."""
    print('not empty');

not_empty_lambda = lambda: print('not empty')

not_empty_lambda_with_docstring = lambda: print('not empty')
not_empty_lambda_with_docstring.__doc__ = """this is just an example docstring."""

#
# Not empty functions returning a string (expect: is_empty_function(f) == False)
#

def not_empty_returning_string():
    return 'not empty'

def not_empty_returning_string_with_docstring():
    return 'not empty'

not_empty_lambda_returning_string = lambda: 'not empty'

not_empty_lambda_returning_string_with_docstring = lambda: 'not empty'
not_empty_lambda_returning_string_with_docstring.__doc__ = """this is just an example docstring."""


all([
  is_empty_function(empty) == True,
  is_empty_function(empty_with_docstring) == True,
  is_empty_function(empty_lambda) == True,
  is_empty_function(empty_lambda_with_docstring) == True,

  is_empty_function(not_empty) == False,
  is_empty_function(not_empty_with_docstring) == False,
  is_empty_function(not_empty_lambda) == False,
  is_empty_function(not_empty_lambda_with_docstring) == False,

  is_empty_function(not_empty_returning_string) == False,
  is_empty_function(not_empty_returning_string_with_docstring) == False,
  is_empty_function(not_empty_lambda_returning_string) == False,
  is_empty_function(not_empty_lambda_returning_string_with_docstring) == False,

]) # == True
like image 76
Ente Avatar answered Sep 30 '22 16:09

Ente


The method you propose does not quite work because empty functions that have docstrings have a slightly different bytecode.

The value of func.__code__.co_code for an empty function with no docstring is 'd\x00\x00S', while the value of it for a function with a docstring is 'd\x01\x00S'.

For my purposes, it works just to add the additional case to test for:

def isEmptyFunction(func):
    def empty_func():
        pass

    def empty_func_with_doc():
        """Empty function with docstring."""
        pass

    return func.__code__.co_code == empty_func.__code__.co_code or \
        func.__code__.co_code == empty_func_with_doc.__code__.co_code
like image 41
kcon Avatar answered Sep 30 '22 16:09

kcon