954,206 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Return a class in function definition???

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

jliu66
Light Poster
30 posts since Oct 2007
Reputation Points: 10
Solved Threads: 0
 

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

woooee
Nearly a Posting Maven
2,454 posts since Dec 2006
Reputation Points: 777
Solved Threads: 714
 

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.

jliu66
Light Poster
30 posts since Oct 2007
Reputation Points: 10
Solved Threads: 0
 

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

jrcagle
Practically a Master Poster
608 posts since Jul 2006
Reputation Points: 92
Solved Threads: 156
 

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

jliu66
Light Poster
30 posts since Oct 2007
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You