INNER CODE UNIT · Python

__repr__

webartifex/intro-to-python · 11_classes/sample_package/matrix.py:88

    def __repr__(self):
        """Text representation of a Matrix."""
        name = self.__class__.__name__
        args = ", ".join(
            "(" + ", ".join(repr(c) for c in r) + ",)" for r in self._entries
        )
        return f"{name}(({args}))"

    def __str__(self):
        """Human-readable text representation of a Matrix."""
        name = self.__class__.__name__
        first, last, m, n = self[0], self[-1], self.n_rows, self.n_cols
        return f"{name}(({first!r}, ...), ..., (..., {last!r}))[{m:d}x{n:d}]"

    @property
    def n_rows(self):
        """Number of rows in a Matrix."""
        return len(self._entries)

View source record →

📰 Research Paper
Loading…
⏳ Fetching content…