我一直在阅读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()函数表示了极大的赞赏,当传递一串任意字节时,它将打印出人类可读的十六进制格式.最近我一直在用C做一些工作,我开始想念python repr函数.我一直在互联网上寻找与它类似的东西,最好是类似的东西void buffrepr(const char * buff, const int size, char * result, const int resultSize)但我没有运气,是否有人知道这样做的简单方法?
什么<function at 'somewhere'>意思?例:
>>> def main():
... pass
...
>>> main
<function main at 0x7f95cf42f320>
Run Code Online (Sandbox Code Playgroud)
也许有办法以某种方式使用它0x7f95cf42f320?
这不是我真正得到的。我试图用来__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) 假设我想调试一个带有属性的简单类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限制 - 或者还有什么?
我有一个问题,有没有办法“强制”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()用于不适合的目的。
如果我创建一个深度嵌套的列表,像这样:
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" % arr和repr(arr)也.)
我怎么能得到打印的字符串?这种差异的根本原因是什么?
我尝试__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) __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 ×10
repr ×10
c ×1
difference ×1
escaping ×1
exception ×1
function ×1
inheritance ×1
printing ×1
python-2.7 ×1
python-3.x ×1
quotes ×1
recursion ×1
self ×1
unicode ×1