0

well I have this code returns me a list:

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()

but sometimes it returns the object variable and it returns me this:

<__main__.ListObject instance at 0x16917e8>

What am I doing wrong?

3
  • Sometimes? This code always seems to return it. Commented May 12, 2014 at 12:32
  • What did you expect to be printed instead? Commented May 12, 2014 at 12:33
  • this: [MibObject('MY-MIB', 'testDescription', mib.getTestDescription), MibObject('MY-MIB', 'testCount', mib.getTestCount),MibObject('MY-MIB', 'testGet', mib.getTestGet), MibObject('MY-MIB', 'testSet', mib.getTestSet) ] Commented May 12, 2014 at 12:36

1 Answer 1

1

When you pass an object to python print, the printed string is given by its class 'str' method.

<main.ListObject instance at 0x16917e8> is the string returned by the default implementation of __str__ method. If you haven't overridden this method for your class, the result is just the expected.

EDIT:

If what you want is to print objects when you write print ListObject() you have to override ListObject __str__(self) method:

class ListObject:
    def __str__(self):
        return self.objects.__str__()
Sign up to request clarification or add additional context in comments.

3 Comments

understand, but it has some way of returning that object list?
@PauloVagner Edited my answer override __str__.
Thank you, you were a big help !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.