labrad / pylabrad

python interface for labrad
50 stars 31 forks source link

DimensionlessFloat is not hashable #380

Open kylinzzx opened 3 years ago

kylinzzx commented 3 years ago

DimensionlessFloat can not be called by hash(obj) I am not sure this is a designed behavior.

import labrad.units as U
a = U.Value(3, 'ns')
type(a['ns'])  # float
hash(a['ns'])  # no error, float number is hashable.

b = U.Value(3, 'ns')
c = U.Value(1, 'GHz')
print(type(b*c))  # DimensionlessFloat
print(hash(b*c))  
# Type Error  
# unhashable type: 'DimensionlessFloat'
fanmingyu212 commented 3 years ago

One way to solve this problem might be implementing __hash__ method in the WithDimensionlessUnit class (https://github.com/labrad/pylabrad/blob/3f4d4849ca101de1edfeb6959ee8ceccb821251d/labrad/units.py#L979). Currently the method is only implemented in the WithUnit class.