what is the Pythonic way to implement a __str__ method with different format options?
问题 I'd like to create a __str__ method that creates the string in various formats according to user choice. The best I have come up with is to make a __str__(**kwargs) method, and this seems to work ok, but it isn't compatible with str(obj) or print(obj) . In other words I have to use print(obj.__str__(style='pretty')) rather than print(obj, style='pretty') . 回答1: Implement the object.__format__() method instead, and a user can then specify the formatting required with the format() function and