polytope icon indicating copy to clipboard operation
polytope copied to clipboard

Implement __repr__ for custom classes

Open carterbox opened this issue 9 years ago • 3 comments

Recently I discovered the __repr__ function in python. It seems that it would be useful implement this function such that objects can be recreated from log outputs.

http://stackoverflow.com/a/19597196

carterbox avatar Feb 14 '17 22:02 carterbox

more motivation: entering a variable name at the IPython prompt causes its __repr__ function to be called.

slivingston avatar Feb 15 '17 02:02 slivingston

Creating __repr__ for the class Polytope might be difficult if you want to include the memoized value of the bounding box because it (the attribute bbox) cannot be assigned by calling __init__.

slivingston avatar Feb 15 '17 02:02 slivingston

Following the previous comment and to simplify __repr__, I suggest omitting all attributes that are computed automatically from other values. These comprise dependent information (for the sake of the argument only, we could imagine recomputing them with better precision on a future machine).

__repr__ is a useful mechanism, though I rarely use it. So, it would probably be good to implement this magic method only if someone uses it somewhere. The use case that seems more interesting is data persistence.

Relevant issues: https://github.com/tulip-control/tulip-control/issues/59 https://github.com/tulip-control/tulip-control/issues/69

johnyf avatar Feb 17 '17 22:02 johnyf