[Tutor] Newbie Question on Exceptions...
I'm working my way through the book "beginning python" and I came across an exercise that suggests using Exception trapping to see if a value is in a dictionary: fridge={"apple":"A shiny red apple","pear":"a nice ripe pear","grapes":"seadless grapes"} food_sought="apple" fridge_list=fridge.keys(); try: print "The fridge contains %s" %fridge[food_sought] except (KeyError): print "The fridge does not contain %s"%food_sought I'm fairly certain the book is in error in calling this a "short-cut" since the has_key method is much less verbose to use, but it brings up a question about exceptions in general: In Java using exceptions in the way shown above is a classic anti-pattern since Exceptions should only be used for..well exceptional conditions. Is the same true of Python? Or is ok to use Exception handling like the book suggests? Thanks in advance, David Hamilton ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Learning to Debug?
I'm moving forward with my learning of Python, but I've decided it's finally time to address a problem that has haunted me in every language I've every tried to learn: debugging. I'm just not very good at it. Does anyone have recommendations for Python-centric books/online tutorials that teach you techniques for good debugging? Thanks, David Hamilton ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Design Question...
I'm doing the initial design for what will (hopefully) be something like a flash-card system. This is mostly a learning exercise, but I'm hoping the results will be at least usable. So my question is does this class design shown by the psudo-code below sound reasonable? class Deck(Questions): Contains a set of questions, and provides information and utility functions for them methods: add_question(Question) -- Ads a question to the deck get_next() -- Returns the next question in the deck ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Design Question
I think I may have sent an incomplete version of this question a moment ago (sorry). Here is the complete question: I'm designing something along the lines of a flash card program. It's mostly just an exercise in learning Python, but I'd like it to be at least marginally usable when I'm done. So I'm looking for comments/suggestions on the key pieces of the design: the questions and the flash card deck: Psudo-code of current design: class Deck(): """Provides managment and informational functions about a set of questions to be asked methods incldue: __init__(questions) -- takes a list of question and creates a new deck with these questions. add_question(self,question) -- Adds a question to the current deck remove_question(self,question) -- returns True if the question was removed, False otherwise get_question() -- Returns the next unanswered question in the deck get_stats() -- returns a tuple containing: number_asked, number_correct, number_remaining shuffle_deck() -- shuffles the order of the remaining questions. Deck Overrived the __len__ function so that the len returned is the number of questions in the deck." class Question(): """Provides questions to be asked methods: __init__(self,question,answer) -- question string representing the question. answer can be a text string, a tupple (for multiple correct answers) or an object implementing an is_correct() method that returns a boolean check_answer(self,answer) -- tests to see if the answer is correct and returns a boolean """ Mostly I'm wondering, is this over-kill? The idea is to allow for the deck to be used for as wide a variety of purposes as possible. Also, I want to make it easy to write code that generates decks. Is this design over-kill? Any comments/suggestions welcome. Thanks, David ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor