本文介绍了反引号对python解释器意味着什么:`num`的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在玩列表理解,并且在另一个站点上遇到了这个小片段:

I'm playing around with list comprehensions and I came across this little snippet on another site:

return ''.join([`num` for num in xrange(loop_count)])

我花了几分钟的时间尝试通过键入来复制功能(通过键入),然后才意识到`num`位将其破坏.

I spent a few minutes trying to replicate the function (by typing) before realising the `num` bit was breaking it.

将这些语句括在这些字符中有什么作用?从我所看到的相当于str(num).但是当我计时的时候:

What does enclosing a statement in those characters do? From what I can see it is the equivalent of str(num). But when I timed it:

return ''.join([str(num) for num in xrange(10000000)])

需要4.09秒,而:

return ''.join([`num` for num in xrange(10000000)])

需要2.43秒.

两者都给出相同的结果,但结果要慢得多.这是怎么回事?

Both give identical results but one is a lot slower. What is going on here?

编辑:奇怪的是... repr()给出的结果比`num`稍慢. 2.99秒和2.43秒.使用Python 2.6(尚未尝试过3.0).

Oddly... repr() gives slightly slower results than `num`. 2.99s vs 2.43s. Using Python 2.6 (haven't tried 3.0 yet).

推荐答案

反引号是repr()的不推荐使用的别名.不再使用它们,该语法已在Python 3.0中删除.

Backticks are a deprecated alias for repr(). Don't use them any more, the syntax was removed in Python 3.0.

在2.x版中,使用反引号似乎比使用repr(num)num.__repr__()更快.我猜是因为分别在全局名称空间(对于repr)或对象的名称空间(对于__repr__)中需要附加的字典查找.

Using backticks seems to be faster than using repr(num) or num.__repr__() in version 2.x. I guess it's because additional dictionary lookup is required in the global namespace (for repr), or in the object's namespace (for __repr__), respectively.

使用dis模块证明了我的假设:

Using the dis module proves my assumption:

def f1(a):
    return repr(a)

def f2(a):
    return a.__repr__()

def f3(a):
    return `a`

拆解节目:

>>> import dis
>>> dis.dis(f1)
  3           0 LOAD_GLOBAL              0 (repr)
              3 LOAD_FAST                0 (a)
              6 CALL_FUNCTION            1
              9 RETURN_VALUE
>>> dis.dis(f2)
  6           0 LOAD_FAST                0 (a)
              3 LOAD_ATTR                0 (__repr__)
              6 CALL_FUNCTION            0
              9 RETURN_VALUE        
>>> dis.dis(f3)
  9           0 LOAD_FAST                0 (a)
              3 UNARY_CONVERT       
              4 RETURN_VALUE   

f1涉及针对repr的全局查找,f2涉及针对__repr__的属性查找,而反引号运算符是在单独的操作码中实现的.由于没有字典查找(LOAD_GLOBAL/LOAD_ATTR)和函数调用(CALL_FUNCTION)的开销,因此反引号会更快.

f1 involves a global lookup for repr, f2 an attribute lookup for __repr__, whereas the backtick operator is implemented in a separate opcode. Since there is no overhead for dictionary lookup (LOAD_GLOBAL/LOAD_ATTR) nor for function calls (CALL_FUNCTION), backticks are faster.

我猜想Python专家认为对repr()进行单独的低级操作是不值得的,并且同时repr()和反引号都违反了该原理

I guess that the Python folks decided that having a separate low-level operation for repr() is not worth it, and having both repr() and backticks violates the principle

因此该功能已在Python 3.0中删除.

so the feature was removed in Python 3.0.

这篇关于反引号对python解释器意味着什么:`num`的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 09:56