2

I have a Python class ElectionData that stores all data for an election, including a list of ElectionRound instances that in turn store the vote counts for each round.

For debugging and verbose printing purposes, I would like an option to fully print all of the data within ElectionData, including the vote counts in each contained ElectionRound. This printout should be human-readable, formatting the information with newlines.

My understanding is that __repr__() is for an unambiguous system representation of the instance and __str()__ is for a more concise user representation, but I am not sure if it is meant for large representations.

Given that this "pretty" formatted printout could be quite long, is using __str()__ still advised, or should I create a new method like description() for this purpose?

Edit:

I have already researched the difference between __str__() and __repr__(); this question is about if a long-form printout should use __str__() or a separate method

dgund
  • 3,459
  • 4
  • 39
  • 64
  • 3
    `__str__` should be concise (one line). Define a new method, called `tabulate` or something. – wim Mar 20 '17 at 20:17
  • Possible duplicate of [Difference between \_\_str\_\_ and \_\_repr\_\_ in Python](http://stackoverflow.com/questions/1436703/difference-between-str-and-repr-in-python) – kͩeͣmͮpͥ ͩ Mar 20 '17 at 20:17
  • 3
    I'd go with writing your own `description()` method – secondbreakfast Mar 20 '17 at 20:19
  • 1
    You can invert the control: instead calling a method like `instance.fancy_print()`, you can define a function and use it: `fancy_print(instance)`. This allows you to put more logic into the function and less into the instance method(s) it calls, and use a short alias for it in your REPL (`p = fancy_print; p(x)`) without making compromises between descriptiveness and conciseness in naming your instance methods. – 9000 Mar 20 '17 at 20:28

0 Answers0