Skip to content
Advertisement

Is there a chain calling method in Python?

Is there a function in Python that would do this:

JavaScript

by typing this (for example):

JavaScript

I just figured, since python is (arguably) a functional language the function I’m looking for will make syntax brighter

Advertisement

Answer

Use the reduce() function to chain calls:

JavaScript

I used the forward-compatible functools.reduce() function; in Python 3 reduce() is no longer in the built-in namespace.

This can also be made a separate function, of course:

JavaScript
Advertisement