Python: repr vs backquote

依然范特西╮ 提交于 2019-12-10 12:39:52

问题


In python, is there a difference between repr and the backquote ` (left of 1)?

For demonstration:

class A(object):
    def __repr__(self):
        return 'repr A'
    def __str__(self):
        return 'str A'


>>> a = A()

>>> repr(a)
#'repr A'

>>> `a`
#'repr A'

>>> str(a)
#'str A'

Do the backquotes just call repr? Is it simply for convenience? Is there any significant speed difference?

Thanks!


回答1:


They're an alias for repr. They have the exact same effect.

However, they're deprecated and have been removed in Python 3. Don't use them; use repr.




回答2:


According to python.org covering repr:

This is the same value yielded by conversions (reverse quotes).

It should be noted that the backtick method is considered something of an abomination by the language designers at the moment, and it was removed in python 3.



来源:https://stackoverflow.com/questions/7490261/python-repr-vs-backquote

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!