Return a class in function definition???

Thread Solved

Join Date: Oct 2007
Posts: 30
Reputation: jliu66 is an unknown quantity at this point 
Solved Threads: 0
jliu66 jliu66 is offline Offline
Light Poster

Return a class in function definition???

 
0
  #1
Oct 24th, 2007
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
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,008
Reputation: woooee is a jewel in the rough woooee is a jewel in the rough woooee is a jewel in the rough 
Solved Threads: 285
woooee woooee is offline Offline
Veteran Poster

Re: Return a class in function definition???

 
0
  #2
Oct 24th, 2007
Code something up and try it for yourself. If you get an error message then post the code here for some help.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 30
Reputation: jliu66 is an unknown quantity at this point 
Solved Threads: 0
jliu66 jliu66 is offline Offline
Light Poster

Re: Return a class in function definition???

 
0
  #3
Oct 24th, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 608
Reputation: jrcagle is on a distinguished road 
Solved Threads: 150
jrcagle jrcagle is offline Offline
Practically a Master Poster

Re: Return a class in function definition???

 
0
  #4
Oct 24th, 2007
This code runs fine:

  1. def ols(x, y):
  2.  
  3. class output:
  4. pass
  5.  
  6. results = output()
  7.  
  8. results.method = 'ols'
  9. results.y = y
  10. #results.nobs = n
  11. #results.nvar = nvar
  12. #results.beta = B
  13.  
  14. return results
  15.  
  16. r = ols(1,2)
  17. print r

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

Jeff
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 30
Reputation: jliu66 is an unknown quantity at this point 
Solved Threads: 0
jliu66 jliu66 is offline Offline
Light Poster

Re: Return a class in function definition???

 
0
  #5
Oct 28th, 2007
Originally Posted by jrcagle View Post
This code runs fine:

  1. def ols(x, y):
  2.  
  3. class output:
  4. pass
  5.  
  6. results = output()
  7.  
  8. results.method = 'ols'
  9. results.y = y
  10. #results.nobs = n
  11. #results.nvar = nvar
  12. #results.beta = B
  13.  
  14. return results
  15.  
  16. r = ols(1,2)
  17. 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
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC