class/str

This commit is contained in:
Marc Beninca 2024-09-16 21:41:45 +02:00
parent 487d7cb9b2
commit 8227524a7c
Signed by: marc.beninca
GPG key ID: 9C7613450C80C24F

View file

@ -2,14 +2,16 @@
__version__ = "0.0.1" __version__ = "0.0.1"
from os import linesep
class Class: class Class:
"""Root class.""" """Root class."""
def __repr__(self) -> str: def __repr__(self) -> str:
"""Represent object variables. """Return machine-readable state.
:return: representation :return: state
:rtype: str :rtype: str
""" """
name = self.__class__.__name__ name = self.__class__.__name__
@ -18,3 +20,14 @@ class Class:
] ]
arguments = ", ".join(attributes) arguments = ", ".join(attributes)
return f"{name}({arguments})" return f"{name}({arguments})"
def __str__(self) -> str:
"""Return human-readable state.
:return: state
:rtype: str
"""
attributes = [
f"{k} = {v}" for k, v in vars(self).items() if not k.startswith("_")
]
return linesep.join(attributes)