返回列表函数python
我是python新手,我正在尝试下面的代码q Objects1 return to list 我该怎么做 它返回以下错误 文件“/home/paulo/Desktop/testepy2/objectMIB.py”,第53行 返回 SyntaxError:函数外部的“return” 多谢各位返回列表函数python,python,Python,我是python新手,我正在尝试下面的代码q Objects1 return to list 我该怎么做 它返回以下错误 文件“/home/paulo/Desktop/testepy2/objectMIB.py”,第53行 返回 SyntaxError:函数外部的“return” 多谢各位 from pysnmp.entity import engine, config from pysnmp import debug from pysnmp.entity.rfc3413 import cmdr
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):
self._test_set = value
class ListObejtc ():
mib = objectMIB.Mib()
objects1 = [MibObject('MY-MIB', 'testDescription', mib.getTestDescription),
MibObject('MY-MIB', 'testCount', mib.getTestCount),MibObject('MY-MIB', 'testGet', mib.getTestGet), MibObject('MY-MIB', 'testSet', mib.getTestSet) ]
print objects1
return
嵌套在“
ListObejtc
”中的代码在方法中是正常的,如下所示:
class ListObejtc ():
def __init__(self):
pass
def doObjects(self):
mib = objectMIB.Mib()
objects1 = [MibObject('MY-MIB', 'testDescription', mib.getTestDescription),
MibObject('MY-MIB', 'testCount', mib.getTestCount),MibObject('MY-MIB', 'testGet', mib.getTestGet), MibObject('MY-MIB', 'testSet', mib.getTestSet) ]
print objects1
return objects1
你得到了一个
SyntaxError
,因为你得到的return
是在类上下文中,没有任何意义。你在类上下文中添加了一个return
语句,这确实是一个SyntaxError。另外,请注意缩进,您发布的代码有一些缩进问题。如果您是python新手,我想给您一条建议:始终阅读错误回溯,它通常提供有关错误的清晰信息,您可以使用它来解决itAn\uuu init\uuu()
方法应该进行初始化,它不应该返回任何内容。\uuuu init\uuuu
tsk,tsk中不应该有return语句。相当粗心。更正。谢谢,但现在它返回以下错误文件“/home/paulo/Desktop/testepy2/objectMIB.py”,第50行通过^IndentationError:应为缩进块