为什么repr()会添加转义字符?

时间:2017-08-12 22:23:07

标签: python python-3.x repr

来自下面的clock装饰者,

import time
from functools import wraps

DEFAULT_FMT = '[{elapsed:0.8f}s] {name}({args}) -> {result}'

def clock(fmt=DEFAULT_FMT):
    def decorate(func):
        @wraps(func)
        def wrapper(*args):
            t0 = time.time()
            result = func(*args)
            elapsed = time.time() - t0
            name=func.__name__
            args = ','.join(repr(arg) for arg in args)
            result=repr(result)
            print(fmt.format(**locals()))
            return result
        return wrapper
    return decorate

result=repr(result)在输出字符串中添加了转义\\\字符,代码如下,

from functools import lru_cache
from clock_package import clock

emptyRecrList = None

#Representation - start
#Constructor
def list(first, rest):
    return [first, rest]

def first(s):
    assert s != empty, 'empty linked list has no first'
    return s[0]

def rest(s):
    assert s != empty, 'empty linked list has no rest'
    return s[1]
#Representation - end


def fibSequence(k:int) -> list:
    prev, curr = 1, 0
    @lru_cache()           # applied last
    @clock()               # applied first
    def genSequence(prev, curr, k):
        if k == 0:
            return emptyRecrList
        elif k == 1:
            return list(curr, emptyRecrList)
        else:
            return list(curr, genSequence(curr, prev+curr, k-1))
    return genSequence(prev, curr, k)

if __name__ == '__main__':
    fibSequence(11)

预期产出:

$ python3.6 fibonacci.py 
[0.00000167s] genSequence(34,55,1) -> [55, None]
[0.00007153s] genSequence(21,34,2) -> [34, [55, None]]
[0.00010061s] genSequence(13,21,3) -> [21, [34, [55, None]]]
[0.00012279s] genSequence(8,13,4) -> [13, [21, [34, [55, None]]]]
[0.00014472s] genSequence(5,8,5) -> [8, [13, [21, [34, [55, None]]]]]
[0.00016618s] genSequence(3,5,6) -> [5, [8, [13, [21, [34, [55, None]]]]]]
[0.00018954s] genSequence(2,3,7) -> [3, [5, [8, [13, [21, [34, [55, None]]]]]]]
[0.00021267s] genSequence(1,2,8) -> [2, [3, [5, [8, [13, [21, [34, [55, None]]]]]]]]
[0.00023627s] genSequence(1,1,9) -> [1, [2, [3, [5, [8, [13, [21, [34, [55, None]]]]]]]]]
[0.00026131s] genSequence(0,1,10) -> [1, [1, [2, [3, [5, [8, [13, [21, [34, [55, None]]]]]]]]]]
[0.00028825s] genSequence(1,0,11) -> [0, [1, [1, [2, [3, [5, [8, [13, [21, [34, [55, None]]]]]]]]]]]

实际输出:

$ python3.6 fibonacci.py 
[0.00000143s] genSequence(34,55,1) -> [55, None]
[0.00007296s] genSequence(21,34,2) -> [34, '[55, None]']
[0.00011134s] genSequence(13,21,3) -> [21, "[34, '[55, None]']"]
[0.00013995s] genSequence(8,13,4) -> [13, '[21, "[34, \'[55, None]\']"]']
[0.00016642s] genSequence(5,8,5) -> [8, '[13, \'[21, "[34, \\\'[55, None]\\\']"]\']']
[0.00019479s] genSequence(3,5,6) -> [5, '[8, \'[13, \\\'[21, "[34, \\\\\\\'[55, None]\\\\\\\']"]\\\']\']']
[0.00022340s] genSequence(2,3,7) -> [3, '[5, \'[8, \\\'[13, \\\\\\\'[21, "[34, \\\\\\\\\\\\\\\'[55, None]\\\\\\\\\\\\\\\']"]\\\\\\\']\\\']\']']
[0.00025082s] genSequence(1,2,8) -> [2, '[3, \'[5, \\\'[8, \\\\\\\'[13, \\\\\\\\\\\\\\\'[21, "[34, \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\'[55, None]\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\']"]\\\\\\\\\\\\\\\']\\\\\\\']\\\']\']']
[0.00027943s] genSequence(1,1,9) -> [1, '[2, \'[3, \\\'[5, \\\\\\\'[8, \\\\\\\\\\\\\\\'[13, \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\'[21, "[34, \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\'[55, None]\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\']"]\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\']\\\\\\\\\\\\\\\']\\\\\\\']\\\']\']']
[0.00030875s] genSequence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\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\'[55, None]\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\']"]\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\']\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\']\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\']\\\\\\\\\\\\\\\']\\\\\\\']\\\']\']']

为什么result=repr(result)在输出字符串中添加转义字符?

2 个答案:

答案 0 :(得分:2)

您遇到的问题是您的clock装饰器正在更改您的函数返回的类型。我怀疑你打算让行result=repr(result)仅影响当前行的打印,但它会影响后面的行,因为当返回result时,它是一个字符串而不是列表。当它包含在后面的结果中时,引号冲突,Python开始使用反斜杠来逃避它们。然后它需要更多的反斜杠来逃避之前的反斜杠,最终导致你所显示的混乱。

尝试使用repr'结果的其他变量名为print,对于未经修改的结果return

    def wrapper(*args):
        t0 = time.time()
        raw_result = func(*args)               # use a different variable name here
        elapsed = time.time() - t0
        name=func.__name__
        args = ','.join(repr(arg) for arg in args)
        result=repr(raw_result)                # convert to string for display
        print(fmt.format(**locals()))
        return raw_result                      # return the unmodified version

答案 1 :(得分:0)

repr返回一个字符串 - 当复制/粘贴到Python源代码中时 - 将创建相同的值。当您想要查看人类可读的字符串时,您可能希望使用str