Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: repr vs backquote

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!

like image 845
TorelTwiddler Avatar asked Sep 20 '11 18:09

TorelTwiddler


2 Answers

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.

like image 76
agf Avatar answered Sep 23 '22 04:09

agf


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.

like image 41
jkerian Avatar answered Sep 21 '22 04:09

jkerian