Skip to content
5 changes: 5 additions & 0 deletions src/api/printable_result.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,16 @@
class PrintableResult:
def __init__(self, result: Result):
self._result = result
self.has_printed = False

def print(self):
"""Prints the result to the console."""
if self.has_printed:
return

stringifier = ConsoleStringifier(c.configuration.to_stringifier_config())
print(stringifier.result_to_str(self._result))
self.has_printed = True

def to_latex_str(self) -> str:
"""Converts the result to a string that can be used in LaTeX documents.
Expand Down
3 changes: 2 additions & 1 deletion src/api/res.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ def res(
stat: Union[float, int, str, Decimal, None] = None,
sigfigs: Union[int, None] = None,
decimal_places: Union[int, None] = None,
print: Union[bool, None] = None, # pylint: disable=redefined-builtin
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of just disabling this error message, shouldn't we rename print to something different to avoid overriding the internal print method accidentally? E.g. use should_print.

) -> PrintableResult:
"""
Declares your result. Give it a name and a value. You may also optionally provide
Expand Down Expand Up @@ -84,7 +85,7 @@ def res(

# Print automatically
printable_result = PrintableResult(result)
if c.configuration.print_auto:
if (c.configuration.print_auto and print is not False) or print is True:
printable_result.print()

# Export automatically
Expand Down