944,050 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Marked Solved
  • Views: 4074
  • Python RSS
Oct 24th, 2007
0

Return a class in function definition???

Expand Post »
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
Reputation Points: 10
Solved Threads: 0
Light Poster
jliu66 is offline Offline
30 posts
since Oct 2007
Oct 24th, 2007
0

Re: Return a class in function definition???

Code something up and try it for yourself. If you get an error message then post the code here for some help.
Reputation Points: 741
Solved Threads: 692
Nearly a Posting Maven
woooee is offline Offline
2,307 posts
since Dec 2006
Oct 24th, 2007
0

Re: Return a class in function definition???

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.
Reputation Points: 10
Solved Threads: 0
Light Poster
jliu66 is offline Offline
30 posts
since Oct 2007
Oct 24th, 2007
0

Re: Return a class in function definition???

This code runs fine:

Python Syntax (Toggle Plain Text)
  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
Reputation Points: 92
Solved Threads: 156
Practically a Master Poster
jrcagle is offline Offline
608 posts
since Jul 2006
Oct 28th, 2007
0

Re: Return a class in function definition???

Click to Expand / Collapse  Quote originally posted by jrcagle ...
This code runs fine:

Python Syntax (Toggle Plain Text)
  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
Reputation Points: 10
Solved Threads: 0
Light Poster
jliu66 is offline Offline
30 posts
since Oct 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: how to disable Copy/Cut/Paste on a textbox?
Next Thread in Python Forum Timeline: xmlrpc register multiple instances





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC