Skip to content

Quantity is mutable but defines a __hash__ method #761

@lpsinger

Description

@lpsinger

The astropy.units.Quantity class is mutable because its value and unit properties both have setters, but it also declares a __hash__ method. Usually, only immutable objects should be hashable.

Here are two distinct Quantities:

>>> from astropy import units as u
>>> a = 5 * u.second
>>> b = 6 * u.second

And here they are as keys of a dictionary:

>>> c = {a: 'foo', b: 'bar'}
>>> c
{<Quantity 5 s>: 'foo', <Quantity 6 s>: 'bar'}

Since the two Quantities are mutable, we can change the properties of one of them to make them equal and render one of the dictionary values inaccessible:

>>> a.value = 6
>>> c
{<Quantity 6 s>: 'foo', <Quantity 6 s>: 'bar'}
>>> c[a]
'bar'
>>> c[b]
'bar'

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions