【问题标题】:what method determines ipython return, it isn't str or unicode什么方法确定 ipython 返回,它不是 str 或 unicode
【发布时间】:2016-02-09 15:20:54
【问题描述】:

这些不匹配,68 的返回...注意没有 unicode,在 ipython 中使用 python3:

In [2]: from datetime import date

In [3]: d = date(2010, 1, 31)

In [6]: d
Out[6]: datetime.date(2010, 1, 31)

In [7]: d.__
d.__add__           d.__eq__            d.__hash__          d.__new__           d.__rsub__          d.__subclasshook__
d.__class__         d.__format__        d.__init__          d.__radd__          d.__setattr__       
d.__delattr__       d.__ge__            d.__le__            d.__reduce__        d.__sizeof__        
d.__dir__           d.__getattribute__  d.__lt__            d.__reduce_ex__     d.__str__           
d.__doc__           d.__gt__            d.__ne__            d.__repr__          d.__sub__           


In [7]: d.__str__
Out[7]: <method-wrapper '__str__' of datetime.date object at 0x7fb4d5733f30>

In [8]: d.__str__()
Out[8]: '2010-01-31'

In [9]: type(d)
Out[9]: datetime.date

In [10]: d.__class__
Out[10]: datetime.date

910 也不匹配。 ipython 如何获得datetime.date(2010, 1, 31) ,这是我不应该担心的 ipython 事情吗?谢谢

【问题讨论】:

  • 你试过repr() 吗?
  • 不,我刚刚在另一篇文章中了解到,repr 似乎是您想要的普通对象。我被甩掉了,因为我习惯了__str__,然后是 django 中的__unicode__

标签: python python-3.x unicode ipython python-3.4


【解决方案1】:

ipython 使用repr() 函数,调用方法__repr__()

In [1]: from datetime import date

In [2]: d = date(2010, 1, 31)

In [3]: print(repr(d))
datetime.date(2010, 1, 31)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-10-01
    • 2013-08-08
    • 1970-01-01
    • 2021-03-21
    • 1970-01-01
    • 2016-11-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多