print("Device discovered: %s" % remote)
m.append(remote)
print(m)
我得到以下结果。我怎样才能获得与列表名称 m 相同的值
Device discovered: 0013A20041481642 - REMOTE
[<digi.xbee.devices.RemoteZigBeeDevice object at 0x7f42c312ceb8>]
最佳答案
列表内容使用repr()
所包含对象的结果,依次使用 __repr__
对象上的方法。
print()
将对象转换为 str()
,它将使用 __str__
方法(如果存在)__repr__
否则。
你remote
对象(digi.xbee.devices.RemoteZigBeeDevice
实例)只有一个__str__
方法,没有 __repr__
方法。实现后者来自定义对象在列表中的显示方式。
或者,打印列表中的所有单独值:
print(*m)
然后将它们用空格分隔,使用 sep='...'
指定不同的分隔符。
或者您可以在打印之前将列表中的所有对象转换为字符串:
print([str(obj) for obj in m])
并打印字符串列表。
关于python - 将 python 变量附加到列表时,我得到对象值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47273343/