Jump to content

Hi all I 've created a fairly simple program that allows you to write a file of questions and ...


G+_Wesley Kerfoot
 Share

Recommended Posts

Hi all. I've created a fairly simple program that allows you to write a file of questions and answers where the format is simply "question=answer\nquestion=answer" and so on (it expects a file called test_questions in the current directory, but that can be changed). I've heavily commented it since it uses many features of Python that haven't been covered yet, but I thought it might be useful for people to learn from!

https://gist.github.com/anonymous/11074621

Link to comment
Share on other sites

Interesting program that shows a number of advanced Python techniques. I'm not sure about the recursive calls in request_answer though. I usually avoid recursion unless absolutely necessary because of the overhead involved. Is Python's memory manager smart enough to clean up if you exit the program when you're a few levels deep?

Link to comment
Share on other sites

Recursion is generally pretty efficient in Python, since the stack only contains object references. It specifically does not do tail-call optimization, so you shouldn't use recursion for simple looping like you would in Lisp. Memory management on program exit is up to the OS--not your program or the Python runtime.

Link to comment
Share on other sites

If you're in an interactive session, the "process" is the interactive session, and it remains open until you quit. That didn't occur to me at first when I read your question, since I never use the interactive stuff. I generally have my source code open in one window with gvim, and a terminal open to run the program.

Link to comment
Share on other sites

If you use recursion in Python you run the risk of using up memory for each recursive call. Each time you call a function Python pushes some data on to a stack (think of the append method for lists). This enables it to know where to return to when a function wants to return a value to the calling function. It's not as complicated as it sounds and you can google "call stack" if you want to know more. But the point is that if you keep calling a function recursively eventually you will start to have a rather large stack. In Python you will actually get an error if it grows to over 1000 calls. So you have to be careful that you limit that. The way I was using it would require the user to fail at entering a number 1000 times before you get that error :p You can certainly argue that's bad and I won't disagree, but nothing bad can really happen, the program would just exit because of an exception.

 

tl;dr it can use up a lot of memory so be careful with it.

 

Edit: Oh and btw this is for anyone reading the above convo who might be confused.

Link to comment
Share on other sites

 Share

×
×
  • Create New...