标签: repr

repr的输出何时有用?

我一直在阅读reprPython.我想知道输出的应用是什么repr.例如

class A:
 pass

repr(A) ='<class __main__.A at 0x6f570>'

b=A()
repr(b) = '<__main__.A instance at 0x74d78>'
Run Code Online (Sandbox Code Playgroud)

什么时候会对'<class __main__.A at 0x6f570>'或感兴趣'<__main__.A instance at 0x74d78>'

python repr

5
推荐指数
2
解决办法
876
查看次数

c的char*缓冲区的Python样式repr?

在大多数情况下,我使用Python工作,因此我对repr()函数表示了极大的赞赏,当传递一串任意字节时,它将打印出人类可读的十六进制格式.最近我一直在用C做一些工作,我开始想念python repr函数.我一直在互联网上寻找与它类似的东西,最好是类似的东西void buffrepr(const char * buff, const int size, char * result, const int resultSize)但我没有运气,是否有人知道这样做的简单方法?

c python repr

5
推荐指数
1
解决办法
901
查看次数

在Python中,'<function at ...>'是什么意思?

什么<function at 'somewhere'>意思?例:

>>> def main():
...     pass
...
>>> main
<function main at 0x7f95cf42f320>
Run Code Online (Sandbox Code Playgroud)

也许有办法以某种方式使用它0x7f95cf42f320

python function repr memory-address

5
推荐指数
1
解决办法
8549
查看次数

如何使用__repr__从中创建新对象?

这不是我真正得到的。我试图用来__repr__从其输出创建一个新对象。

我有一个OrderedSet类,它包含一个列表和组织它的方法。此类的str方法是

def __str__(self):
    s = "Set contains: "
    for elem in self.list: s += (" '" + elem + "'")
    return s
Run Code Online (Sandbox Code Playgroud)

现在,我应该以__repr__某种方式使用它来实例化一个新对象。喜欢Orderedset second = repr(first)

我可以这样吗?

def __repr__(self):
    return self.list.__str__()
Run Code Online (Sandbox Code Playgroud)

python repr

5
推荐指数
1
解决办法
1854
查看次数

我如何直接使用`str.format`作为`__repr__`?

假设我想调试一个带有属性的简单类myattribute.我创建一个repr这样的方法:

class SimpleClass:
  def __repr__(self):
    return "{0.myattribute}".format(self)
Run Code Online (Sandbox Code Playgroud)

感觉有点多余,所以我更喜欢format直接使用:

class SimpleClass:
  __repr__ = "{0.myattribute}".format
Run Code Online (Sandbox Code Playgroud)

......但是失败了IndexError: tuple index out of range.我理解它format不能访问self论点,但我不明白为什么.

我做错了什么,这是CPython限制 - 或者还有什么?

python repr self string-formatting

5
推荐指数
1
解决办法
167
查看次数

强制 repr() 使用单引号

我有一个问题,有没有办法“强制”repr()在字符串周围创建始终单引号?

当我只使用时会发生这种情况 repr()

print repr("test")
'test'
print repr("test'")
"test'"
print repr("test\"")
'test"'
print repr("test'\"")
'test\'"'
Run Code Online (Sandbox Code Playgroud)

所以最后一个实际上是我想要的,但我不想总是添加\\"以获得单引号。


编辑:我不会将答案标记为已接受,因为正如@martijn-pieters 所指出的那样,我将其repr()用于不适合的目的。

python quotes escaping repr

5
推荐指数
1
解决办法
2324
查看次数

__repr__应该返回字节还是unicode?

在Python 3和Python 2中,__repr__应该返回字节还是unicode?参考和报价是理想的.

这里有一些关于2-3兼容性的信息,但我没有看到答案.

python unicode repr python-2.7 python-3.x

5
推荐指数
1
解决办法
419
查看次数

如何将深层嵌套列表转换为字符串

如果我创建一个深度嵌套的列表,像这样:

arr = [1]
for i in range(1000):
    arr = [arr]
Run Code Online (Sandbox Code Playgroud)

然后

print(arr)
Run Code Online (Sandbox Code Playgroud)

会很好,但是

str(arr)
Run Code Online (Sandbox Code Playgroud)

在超过最大递归深度的情况下失败.("%s" % arrrepr(arr)也.)

我怎么能得到打印的字符串?这种差异的根本原因是什么?

python printing recursion repr difference

5
推荐指数
1
解决办法
77
查看次数

异常派生类的 __repr__ 效果不佳

我尝试__repr__在继承自 Exception 的对象上使用方法。

但什么也没打印出来!

谁能帮忙解释一下为什么吗?

class MyException(Exception):
    def __repr__(self):
        return "MyException Object"


try:
    raise MyException()
except MyException as e:
    print(e)   # shows nothing!
Run Code Online (Sandbox Code Playgroud)

python inheritance exception repr

5
推荐指数
1
解决办法
805
查看次数

如何为函数本身设置repr?

__repr__用于返回对象的字符串表示,但在 Python 中,函数也是对象本身,并且可以具有属性

如何设置__repr__函数的?

在这里看到可以为函数外部的函数设置一个属性,但通常__repr__在对象定义本身内设置一个属性,所以我想在函数定义本身内设置 repr。


我的用例是,我正在使用Tenacity重试具有指数退避功能的网络函数,并且我想记录我最后调用的函数的(信息性)名称。

retry_mysql_exception_types = (InterfaceError, OperationalError, TimeoutError, ConnectionResetError)


def return_last_retry_outcome(retry_state):
    """return the result of the last call attempt"""
    return retry_state.outcome.result()


def my_before_sleep(retry_state):
    print("Retrying {}: attempt {} ended with: {}\n".format(retry_state.fn, retry_state.attempt_number, retry_state.outcome))


@tenacity.retry(wait=tenacity.wait_random_exponential(multiplier=1, max=1200),
                stop=tenacity.stop_after_attempt(30),
                retry=tenacity.retry_if_exception_type(retry_mysql_exception_types),
                retry_error_callback=return_last_retry_outcome,
                before_sleep=my_before_sleep)
def connect_with_retries(my_database_config):
    connection = mysql.connector.connect(**my_database_config)
    return connection
Run Code Online (Sandbox Code Playgroud)

目前retry_state.fn显示<function <lambda> at 0x1100f6ee0>类似于@chepner 所说的内容,但我想向其中添加更多信息。

python repr

5
推荐指数
2
解决办法
230
查看次数