返回列表函数python

时间:2014-05-07 14:23:35

标签: python

我是python的新手,我正在尝试下面的代码q Objects1返回列表

我该怎么做?

它返回以下错误

文件“/ home / paulo / Desktop / testepy2 / objectMIB.py”,第53行 返回 SyntaxError:'return'在函数外部

谢谢

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 

1 个答案:

答案 0 :(得分:0)

嵌套在“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是在课堂上下文中,没有任何意义。