G+_Wesley Kerfoot Posted April 19, 2014 Share Posted April 19, 2014 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 More sharing options...
G+_L I Posted April 19, 2014 Share Posted April 19, 2014 Simple? :) Uses a lot of new/different constructs but it should be a good example to learn from. Link to comment Share on other sites More sharing options...
G+_Wesley Kerfoot Posted April 19, 2014 Author Share Posted April 19, 2014 Lionel D haha, true, it does use some fairly advanced things, but I would be happy to answer any questions about how the code works (also you're free to copy it and change it). Link to comment Share on other sites More sharing options...
G+_Lee Crocker Posted April 19, 2014 Share Posted April 19, 2014 Something wrong with built-in random.shuffle()? Link to comment Share on other sites More sharing options...
G+_Wesley Kerfoot Posted April 19, 2014 Author Share Posted April 19, 2014 Lee Crocker nope I just felt like showing one way of implementing it :p Link to comment Share on other sites More sharing options...
G+_Darryl Medley Posted April 22, 2014 Share Posted April 22, 2014 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 More sharing options...
G+_Lee Crocker Posted April 22, 2014 Share Posted April 22, 2014 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 More sharing options...
G+_Darryl Medley Posted April 22, 2014 Share Posted April 22, 2014 Thanks for the response. I was thinking within Python itself. If your running in the shell, when you exit the program back to the shell does it clear the stack? I would expect that when you close Python itself that the OS would free everything allocated during the session by the runtime. Link to comment Share on other sites More sharing options...
G+_Lee Crocker Posted April 22, 2014 Share Posted April 22, 2014 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 More sharing options...
G+_Wesley Kerfoot Posted April 22, 2014 Author Share Posted April 22, 2014 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 More sharing options...
Recommended Posts