سؤال

i have following python code (a bit simplified, but it did make the same error).

class traffic(object):
    def __init__(self, testObj):
        try:
            <do something>
        except AssertionError:
            sys.exit (1)
    def add(self, phase='TEST'):
        <do something>
    def check(self, phase='TEST'):
        <do something>

class testcase(object):
    def __init__(self):
        try:
            <do something>
        except AssertionError:
            sys.exit (1)
    def addSeqPost(self, cmdObj):
        print "add Seq. for POST"
        cmdObj(phase='POST')

tc = testcase()
test = traffic(tc)
tc.addSeqPost(test.add())

I get the below TypeError:

Traceback (most recent call last):
  File "test.py", line 25, in <module>
    tc.addSeqPost(test.add())
  File "test.py", line 20, in addSeqPost
    cmdObj(phase='POST')
TypeError: 'NoneType' object is not callable

If i change my code to, it works, but it is not what i would like:

    def addSeqPost(self, cmdObj):
        print "add Seq. for POST"
        cmdObj.add(phase='POST')

tc.addSeqPost(test())

I would like to make it more general because the test() could have more methods that i would like to pass into tc.addSeqPost(), like tc.addSeqPost(test.check()).

Thanks in adv. for your time and help

After the help from alKid.

One issue remains, what if i want to pass a parameter with test.check(duration=5)? As soon i do that i got the same TypeError...But i don't want/need to return anything from add!!!

Example:

    ...
    def check(self, phase='TEST', duration=0):
        <do something>

tc = testcase()
test = traffic(tc)
tc.addSeqPost(test.add)
tc.addSeqPost(test.check(duration=5))
هل كانت مفيدة؟

المحلول

test.add() will not return the function, it runs the function and gives back the returned value. Since add doesn't return anything, the object passed is None.

tc = testcase()
test = traffic(tc)
tc.addSeqPost(test.add)

Also, remember that test.add needs two arguments. self and phase. You need to pass both of them.

def addSeqPost(self, cmdObj):
    print "add Seq. for POST"
    cmdObj(self, phase='POST') #pass an instance of `testcase` to the function.

Passing another class's instance might not be what you want to do, but it's just an example.

Hope this helps!

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top