TypeErrors

Benjamin Kaplan benjamin.kaplan at case.edu
Sun Mar 1 14:40:57 EST 2009


On Sun, Mar 1, 2009 at 1:50 PM, Sean Novick <daddysean23 at yahoo.com> wrote:

> Here is the bit of code my error was refering to:
> class phonedb:
>
> some definitons...
>
>     def lookup(self, string):
>         list = []
>         for key in self.shelve.keys():
>             e = self.shelve[key]
>         if cmp(e, string) == 0:
>             list.append(e)
>             return(list)
>
> where would it tell me to return an int?
>
>
cmp(e, string) is actually just a shortcut for e.__cmp__(string). Again, you
should almost never directly call cmp. First of all, newer code should
implement the __eq__ method for this, not cmp. Also, you should just use "if
e == string" instead of cmp or eq. This isn't java- Python has operator
overloading (though it isn't quite as flexible as C++).
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20090301/9f666fec/attachment.html>


More information about the Python-list mailing list