[Tutor] Is there a test for hashability?
James Reynolds
eire1130 at gmail.com
Thu Sep 1 17:58:59 CEST 2011
On Thu, Sep 1, 2011 at 11:37 AM, Hugo Arts <hugo.yoshi at gmail.com> wrote:
> On Thu, Sep 1, 2011 at 5:28 PM, Richard D. Moores <rdmoores at gmail.com>
> wrote:
> > Thanks, James, from your ideas I've come up with this function as a
> > general test for hashibility of any object:
> >
> > def is_hashable(object):
> > try:
> > if hash(object):
> > return True
> > except TypeError:
> > return False
> >
> > But is it? It returns True for ints, floats, sets, tuples, strings,
> > functions; and False for lists
> >
> > Dick
>
> Are you sure? In my testing it returns False for sets, but True for
> frozensets as it should.
>
I agree with hugo, I just tested with all of these:
a = 'a'
b = []
c = 1
d = ()
e = set()
f = frozenset()
it gave the correct response for each
a = 'a' - True
b = [] - False
c = 1 - True
d = () - True
e = set() - False
f = frozenset() - True
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20110901/cc94a8b6/attachment-0001.html>
More information about the Tutor
mailing list