简体   繁体   中英

Return list function python

I am new to python and I'm trying this code below q Objects1 return to list

how can I do this?

it returns me the following error

File "/ home/paulo/Desktop/testepy2/objectMIB.py", line 53 return SyntaxError: 'return' outside function

thank you

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 

It's normal for the code you have shown nested inside " ListObejtc " to be in a method, like so:

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 

You got a SyntaxError because the return as you had it was in class context, and it makes no sense there.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM