i was trying to write a class for a little program i’ve been working on, and i ran into an odd failure of indenting. I was wondering if there’s genuinely something wrong with what i wrote, or if it’s the parser’s fault. This fails in Fedora 15’s python 3.2 package.
def __getitem__(self, key):
if CharacterStats.usesSubStats:
if key in self.subStats:
return self.subStats[key]
elif key in self.stats: #change this to 'if' and it works
return self.stats[key]
else:
raise KeyError(key)
#otherwise we end up right here.
As requested so you can run it: http://pastebin.com/d8yQUm3U
If I understand you correctly, you want to throw a KeyError if
usesSubStats is True and key is not in subStats or if usesSubStats is
False and key is not in stats. So I think the problem is that
if/elif/else chaining doesn’t work like you think it does.
Consider:
produces
which I hope does what’s expected and is the pattern you should keep in mind. Since in your test code useSubStats is True, only the first branch is ever tested:
So your code is behaving like this:
I’m not exactly sure how you think it should be behaving, but it seems like you expect that after the “if key in self.subStats” test fails, execution should move back to the next member of the if/elif/else branch one level up and test that. That’s simply not how it works, though.
There are several easy ways to get the behaviour you want: one is to flatten it so that it’s
instead, which will evaluate to False and so the next branch will be tested, or –as you discovered yourself — make the elif an if, which again leads to that condition being independently tested, or rewrite as in an earlier answer.
Does that make sense? The if/elif/else list describes a series of possibilities, with conditions tested sequentially, and only the branch corresponding to the first true condition (taking the final else as ‘elif 1:’) is executed. You don’t move to the next branch depending on what happens within a branch.