返回列表python
这段代码会返回一个列表:返回列表python,python,Python,这段代码会返回一个列表: from pysnmp.entity import engine, config from pysnmp import debug from pysnmp.entity.rfc3413 import cmdrsp, context, ntforg from pysnmp.carrier.asynsock.dgram import udp from pysnmp.smi import builder import threading import collections
from pysnmp.entity import engine, config
from pysnmp import debug
from pysnmp.entity.rfc3413 import cmdrsp, context, ntforg
from pysnmp.carrier.asynsock.dgram import udp
from pysnmp.smi import builder
import threading
import collections
import time
MibObject = collections.namedtuple('MibObject', ['mibName',
'objectType', 'valueFunc'])
class Mib(object):
"""Stores the data we want to serve.
"""
def __init__(self):
self._lock = threading.RLock()
self._test_count = 0
self._test_get = 10
self._test_set = 0
def getTestDescription(self):
return "My Description"
def getTestCount(self):
with self._lock:
return self._test_count
def setTestCount(self, value):
with self._lock:
self._test_count = value
def getTestGet(self):
return self._test_get
def getTestSet(self):
return self._test_set
def setTestSet(self):
with self._lock:
self._test_set = value
class ListObject:
def __init__(self):
mib = Mib()
self.objects = [
MibObject('MY-MIB', 'testDescription', mib.getTestDescription),
MibObject('MY-MIB', 'testCount', mib.getTestCount),
MibObject('MY-MIB', 'testGet', mib.getTestGet),
MibObject('MY-MIB', 'testSet', mib.getTestSet)
]
def returnTest(self):
return ListObject()
class main ():
print ListObject()
但有时它会返回object变量,并返回以下内容:
<__main__.ListObject instance at 0x16917e8>
我做错了什么?将对象传递给python
print
时,打印的字符串由其类“str”方法给出
是\uuu str\uu
方法的默认实现返回的字符串。如果尚未为类重写此方法,则结果只是预期的结果
编辑:
如果您想在编写print ListObject()
时打印对象
,则必须重写ListObject
\u str\u(self)
方法:
class ListObject:
def __str__(self):
return self.objects.__str__()
有时?这段代码似乎总是返回它。您希望打印什么?这:[MibObject('MY-MIB',testDescription',MIB.getTestDescription),MibObject('MY-MIB','testCount',MIB.getTestCount),MibObject('MY-MIB','testGet',MIB.getTestGet),MibObject('MY-MIB','testSet',MIB.getTestSet)]明白,但它有办法返回对象列表吗?@PauloVagner编辑了我的答案覆盖
\uuuu str\uuuu
。谢谢你,你帮了我大忙!