Repository: pandas-dev/pandas
File: pandas/core/col.py (Python)

Code before:
    all_args = []
    if inputs_repr:
        all_args.append(inputs_repr)
    if kwargs_repr:
        all_args.append(kwargs_repr)

    return ", ".join(all_args)


@set_module("pandas.api.typing")
class Expression:
    """
    Class representing a deferred column.

    This is not meant to be instantiated directly. Instead, use :meth:`pandas.col`.
    """

    def __init__(
        self,
        func: Callable[[DataFrame], Any],
        repr_str: str,
        needs_parenthese: bool = False,
    ) -> None:
        self._func = func
        self._repr_str = repr_str
        self._needs_parentheses = needs_parenthese

    def _eval_expression(self, df: DataFrame) -> Any:
        return self._func(df)

    def _with_op(self, op: str, other: Any) -> Expression:
        op_symbol = _OP_SYMBOLS.get(op, op)

        if op == "__getitem__":
            needs_parentheses = False
            repr_str = f"{self!r}[{other!r}]"
        else:
            needs_parentheses = True
            self_repr = f"{self!r}"
            if self._needs_parentheses:
                self_repr = f"({self_repr})"
            other_repr = f"{other!r}"
            if isinstance(other, Expression) and other._needs_parentheses:
                other_repr = f"({other_repr})"

            if op.startswith("__r"):
                repr_str = f"{other_repr} {op_symbol} {self_repr}"
            else:
                repr_str = f"{self_repr} {op_symbol} {other_repr}"

        if isinstance(other, Expression):

Review comment (suggestion):
Renamed because we now use this for `__getitem__` too.

Code after:

    return ", ".join(all_args)


@set_module("pandas.api.typing")
class Expression:
    """
    Class representing a deferred column.

    This is not meant to be instantiated directly. Instead, use :meth:`pandas.col`.
    """

    def __init__(
        self,
        func: Callable[[DataFrame], Any],
        repr_str: str,
        needs_parenthese: bool = False,
    ) -> None:
        self._func = func
        self._repr_str = repr_str
        self._needs_parentheses = needs_parenthese

    def _eval_expression(self, df: DataFrame) -> Any:
        return self._func(df)

    def _with_op(self, op: str, other: Any) -> Expression:
        op_symbol = _OP_SYMBOLS.get(op, op)

        if op == "__getitem__":
            needs_parentheses = False
            repr_str = f"{self!r}[{other!r}]"
        else:
            needs_parentheses = True
            self_repr = f"{self!r}"
            if self._needs_parentheses:
                self_repr = f"({self_repr})"
            other_repr = f"{other!r}"
            if isinstance(other, Expression) and other._needs_parentheses:
                other_repr = f"({other_repr})"

            if op.startswith("__r"):
                repr_str = f"{other_repr} {op_symbol} {self_repr}"
            else:
                repr_str = f"{self_repr} {op_symbol} {other_repr}"

        if isinstance(other, Expression):
            return Expression(
                lambda df: getattr(self._eval_expression(df), op)(
                    other._eval_expression(df)
                ),
                repr_str,