Function Definitions

%reload_ext rubberize
from rubberize import config

A simple function definition is rendered by Rubberize like so:

%%tap
def z(x, y):
    return x**2 + y**2
\( \displaystyle \operatorname{z} \left( x,\, y \right) = x^{2} + y^{2} \)

Multiple-statement Functions

If the function definition has multiple statements within its block:

%%tap
a = 0
Z = 101

def f(x, y):  # This is my function
    """This function does something amaZing."""
    
    a = x**2 * Z
    if Z > 100:
        b = y**2 * Z
    else:
        b = y**2
    return a + b

f(3, 5)
\( \displaystyle a = 0 \)
\( \displaystyle Z = 101 \)
\( \displaystyle \operatorname{f} \left( x,\, y \right) = a + b\ \text{where:} \)
This is my function
\( \displaystyle a = x^{2}\,Z = x^{2} \cdot 101 \)
\( \displaystyle \text{Since}\ Z > 100\ \to\ 101 > 100 \text{:} \)
\( \displaystyle b = y^{2}\,Z = y^{2} \cdot 101 \)
\( \displaystyle \operatorname{f} \left( 3,\, 5 \right) = 3\,434 \)

As seen from the example, Rubberize ignores any function docstring. Similar to a basic function definition, the return statement is placed on the introductory expression as equal to the function signature. However, this time it will be followed by the text “where”.

Rubberize can only handle function definitions with a single return statement as the last line of the block, or piecewise functions.

Variables within the scope of the function definition (x, y, a, and b) are not substituted, even if a already has a value in the global scope. Z however, is substituted since it is only referenced from the global scope.

Functions that Return None

If there is no last return statement or return None is explicitly written, Rubberize will show that the function is equal to an empty set (\(\emptyset\)).

%%tap
def g(x, y):
    a = x**2
    b = y**2
    c = a + b
\( \displaystyle \operatorname{g} \left( x,\, y \right) = \emptyset\ \text{where:} \)
\( \displaystyle a = x^{2} \)
\( \displaystyle b = y^{2} \)
\( \displaystyle c = a + b \)

Piecewise Functions

Multiple return statements are only allowed by Rubberize when the function definition follows a piecewise format, which is a function definition that only contains at least one conditional ladder and an optional last return statement. The following formats are accepted:

Recommended format.
%%tap
def a(x):
    if x < 0:
        return 1
    if 0 < x <= 1:
        return 2
    return 3
Without a final return is also acceptable. Just make sure that all conditions are covered so that the function doesn’t return None.
%%tap
def a(x):
    if x < 0:
        return 1
    if 0 < x <= 1:
        return 2
    if x > 1:
        return 3
Also acceptable, but note that elif and else are unnecessary. See this Pylint message.
%%tap
def a(x):
    if x < 0:
        return 1
    elif 0 < x <= 1:
        return 2
    else:
        return 3
%%tap
def a(x):
    if x < 0:
        return 1
    if 0 < x <= 1:
        return 2
    return 3
\( \displaystyle \operatorname{a} \left( x \right) = \begin{cases} \displaystyle 1, &\text{if}\ x < 0 \\ \displaystyle 2, &\text{if}\ 0 < x \le 1 \\ \displaystyle 3, &\text{otherwise} \end{cases} \)