arrow-py / arrow

🏹 Better dates & times for Python
https://arrow.readthedocs.io
Apache License 2.0
8.71k stars 673 forks source link

Consider changing __repr__ for Arrow #834

Closed systemcatch closed 3 years ago

systemcatch commented 4 years ago

Here are the current str and repr methods for Arrow.

(arrow) chris@ThinkPad:~/arrow$ python
Python 3.8.3 (default, Jul  7 2020, 18:57:36) 
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import arrow
>>> dt=arrow.utcnow()
>>> str(dt)
'2020-07-27T22:38:46.653869+00:00'
>>> print(dt)
2020-07-27T22:38:46.653869+00:00
>>> repr(dt)
'<Arrow [2020-07-27T22:38:46.653869+00:00]>'

__str__ should be human readable which I think is achieved, however __repr__ is designed for debugging and therefore needs to be explicit. It's also nice (but not required) if the __repr__ can be instantiated into a new object.

This would be a breaking change so fits well for 1.0.0 if we decide to do it.

yiransii commented 3 years ago

Hello, I would like to implement this feature. It seems that currently, repr is "[class_name] + str". Can you be more specific with what additional data repr would need to have?

jadchaar commented 3 years ago

We should aim for a __repr__ similar to that of datetime: https://stackoverflow.com/a/19597196/3820660.

systemcatch commented 3 years ago

How about this?

>>> repr(dt)
arrow.Arrow(2020, 7, 27, 22, 38, 46, 653869, tzinfo=tzutc())
jadchaar commented 3 years ago

@yiransii it might be better if @systemcatch takes this on since he has had a design in mind for the repr for a while.

systemcatch commented 3 years ago

We have decided not to proceed with this change.