Newbie: Functions and class
Alex Martelli
aleax at aleax.it
Wed Oct 29 18:30:25 EST 2003
Shu-Hsien Sheu wrote:
...
> def build(self, i):
> self.table[i].setdefault(i, {})
this method has no explicit 'return', so it returns None
> def search(self, i):
> self.table[i] = self.build(i)
So, you're assigning None to self.table[i].
> >>> kk = hittable()
> >>> kk.search(3)
> >>> kk.table
> [{}, {}, {}, None]
>
> I think I can imagine the above code won't work, though couldn't find a
> solution to it.
>
> What am I missing here?
Just forget that wanton assignment in method search! Why would you
want to assign anything new to self.table[i], when self.build(i) has
just modified self.table[i] appropriately?! I.e., change search to:
def search(self, i):
self.build(i)
this doesn't have anything special to do with classes -- rather,
with the care and feeding of dictionaries, I'd say:-).
Alex
More information about the Python-list
mailing list