« first day (2513 days earlier)      last day (715 days later) » 

10:07 AM
in python, is there some way to make the objects of an own class "unhashable"?
import numpy as np
class A:  # to be made unhashable
    def __init__(self, s=3):
        self.some_member = s

sa = set([A(1)])           # fine, but I'd like the same behaviour as below
sn = set([np.ones((1,))])  # TypeError: unhashable type
here A wil always have some defaut __hash__() implementation, but what would I have to do to behave the same as e.g. np.ndarray?
oh you can just add __hash__ = None
 
yeah, and that automatically happens if you define __eq__
 
I just found out that you can still make it hashable after the fact:)
class A:
    def __init__(self, s=3):
        self.some_member = s
    __hash__ = None
a = A()
try:
    set([a])
except TypeError:
    print("not hashable")
A.__hash__ = lambda self: 123
set([a])
print("hashable")
 
yes, but the default object.__hash__ which defers to id is useless in that case, which is why it's disabled
you can also test hashability with hash(a) :P
 
don't accuse me of tying to do useful things:P
oh
 
I mean "in that case" == when you define __eq__
monkeypatching your class is dumb, don't do that
but yes, you can do that
(but not monkeypatching instances with dunder methods, because of how descriptors work...)
class AlwaysTruthy:
    def __bool__(self):
        return True

at = AlwaysTruthy()
at.__bool__ = lambda: False
print(bool(at), at.__bool__())
compare this to AlwaysTruthy.__bool__ = ... which would "work"
 
10:21 AM
oh that's a great example
thank you
what are "descriptors"?
 
a complex lookup system underpinning attribute access
 
ah thanks!
sometimes you get the impressions that the makers of python even thought about what they were doing
 
well it's no haskell, but mostly because they had to stop thinking and start getting actual work done
 
 
1 hour later…
11:38 AM
@AndrasDeak--СлаваУкраїні I played with it some more and tried np.ones((1,)).__hash__ = lambda: 123 but now it tells me that __hash__ is a read-only property. But the implementation of ndarray just defines __hash__ = None, and I thougth the only way to achieve this is via @property
do you have an idea what's going on there?
 
11:53 AM
@flawr where does the implementation "just define __hash__ = None"?
ndarrays are defined in C which usually accounts for magic like this
try numpy.ndarray.__hash__ = ...
 
I just used the editors "jump to definition" function and it pointed me to numpy/core/_multiarray_umath.py
can't find that on github though, only references to it
 
it's an extension module being generated from C
...I think
not sure about that ".py" ending there
yeah, I've got a ~/virtualenvs/py3.9_main/lib/python3.9/site-packages/numpy/core/_multiarray_umath.cpython-39-x86_64-linux-gnu.so shared library as expected
editor might be bullshitting you
 
@AndrasDeak--СлаваУкраїні that does result in the same error btw:)
 
not exactly, read more carefully
 
almost the same TypeError: cannot set '__hash__' attribute of immutable type 'numpy.ndarray'
 
12:07 PM
I get TypeError: can't set attributes of built-in/extension type 'numpy.ndarray'
3.9 here
 
3.10 here
 
So I think what's happening is that ndarray implements __eq__ which disables hash automatically. Since it's a c-extension ("immutable type"?) you can't override it manually either.
 
ah I see
that "immutable type" part just made me curious
thanks for your investigations:)
 
the immutable type seems like a new error message, it rings a bell being mentioned recently in the python room
 

« first day (2513 days earlier)      last day (715 days later) »