2

In (some) python libraries, when i look up the code, i see a bunch of functions or class methods which are written like this:

def funcName():
    pass

I just want to know how those functions DO stuff even though they only have pass which means that the function should do nothing.

IssUseless
  • 47
  • 5

6 Answers6

3

Functions in python syntactically can't be empty. That's why you need to populate them, usually with pass

Sometimes, functions are declared, but purposely left empty. Then, they can be used in the default implementations, but possibly overridden by classes extending (inheriting) the library class.

3

For user-defined functions, pass means doing nothing, but for some library functions (including third-party libraries), they usually mean that their logic is not written in pure Python code, such as NumPy and Python standard library, in order to pursue efficient operation, most functions are written in C language, and provides the corresponding Python interface.

More detailed answer

Mechanic Pig
  • 6,756
  • 3
  • 10
  • 31
1

Those functions/methods with just the pass statement do nothing in particular. pass is usually a placeholder for code that will be written later.

You can also find the pass statement in classes that define custom exceptions, for instance:

class MyException(Exception):
    pass
blunova
  • 2,122
  • 3
  • 9
  • 21
1

Usually these are place holder methods in a (possibly abstract) base class that define a template for derived classes.

timgeb
  • 76,762
  • 20
  • 123
  • 145
1

pass does nothing. It's a filler to let the code run before you implement something. The libraries are probably using pass to:

  • Leave blank code to finish later
  • Create a minimal class:
  • Imply that a section of code does nothing
  • Loop forever (and do nothing)

So to your question about libraries, they probably do nothing. pass on its own is almost as good as a blank line, but can be mixed with functional statements to achieve a working module. But the function in your question does nothing at all.


Say I wanted to keep a function f in my file, and come back to it later. I could use pass to avoid IndentationError:

>>> def f():
...     pass
... 
>>> f()
>>> 

As without pass, I'd have:

>>> def f():
... 
  File "<stdin>", line 2
    
    ^
IndentationError: expected an indented block after function definition on line 1
>>> 

This is also explained in the offical docs:

The pass statement does nothing. It can be used when a statement is required syntactically but the program requires no action. For example:

>>> while True:
...     pass  # Busy-wait for keyboard interrupt (Ctrl+C)
...
Freddy Mcloughlan
  • 4,129
  • 1
  • 13
  • 29
1

Python pass is an empty statement to maintain the integrity of the program structure. Pass does not do anything and is generally used as a placeholder statement. Nothing happens when the pass is executed. It results in no operation.

Suppose we have a loop or a function that is not implemented yet, but we want to implement it in the future. They cannot have an empty body. The interpreter would give an error. So, we use the pass statement to construct a body that does nothing.

kiran
  • 444
  • 3
  • 15