Hi,

I have a question ask you or expert. Can python's function return a class at the end of function definition. The class have only a set of data members like struct in C. Please help and thanks in advance. Any good reference on this topic?

John

Recommended Answers

All 4 Replies

Code something up and try it for yourself. If you get an error message then post the code here for some help.

Here is the code:

def ols(x, y):

class output:
    pass

results = output()

results.method = 'ols'
results.y = y
results.nobs = n
results.nvar = nvar
results.beta = B

return results

It looks like the above is not working with exit code 0 after exeuction. What normally python return exit code after execution? I tested one with exit code 0. I am not sure it is normal or not.

Another thing is there any good debug tool in IDE showing the values in variables during debug? The one IDE coming with Python Win does not have this function. Maybe I am wrong.

This code runs fine:

def ols(x, y):

    class output:
        pass

    results = output()

    results.method = 'ols'
    results.y = y
    #results.nobs = n
    #results.nvar = nvar
    #results.beta = B

    return results

r = ols(1,2)
print r

I think the problem with yours might have been that you were accessing non-existent variables n, nvar, and B.

Jeff

This code runs fine:

def ols(x, y):

    class output:
        pass

    results = output()

    results.method = 'ols'
    results.y = y
    #results.nobs = n
    #results.nvar = nvar
    #results.beta = B

    return results

r = ols(1,2)
print r

I think the problem with yours might have been that you were accessing non-existent variables n, nvar, and B.

Jeff

Thanks so much, Jeff.

John

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.