[Tutor] Making Doubly Linked List with Less Lines of Code.
steve at pearwood.info
Sat Jan 3 03:05:21 CET 2015
On Fri, Jan 02, 2015 at 11:37:12AM -0500, WolfRage wrote:
> On 01/02/2015 02:21 AM, Steven D'Aprano wrote:
> >Your lookup_node method returns a GameTile or False on failure:
> > def lookup_node(self, x, y, ):
> > if not self.check_bounds(x, y):
> > return False
> > return self.grid[y][x]
> >I'm not sure about that design. I wonder whether it would be better to
> >return None, or raise an exception.
> What would you suggest this code does on error, when the Node looked up
> is out of bounds?
In my opinion, the right thing to do is to raise an exception.
In a bug-free program, your users should never see an exception raised.
Either you avoid triggering the exception, or you catch it and recover.
(If you cannot recover, there is no point in catching it.) But if a bug
does exist in your program, then it is probably better to fail hard, as
soon as possible, and provide good diagnostics that you (not your
users!) can use to fix this problem. That is, raise an exception and
print a stacktrace.
That means something like this:
def lookup_node(self, x, y, ):
if not self.check_bounds(x, y):
raise ValueError("out of bounds: %s, %s" % (x, y))
Then have to actually use this method, otherwise it is dead code and
More information about the Tutor