Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dimension: missing dunder methods #2

Open
nstarman opened this issue Mar 10, 2025 · 8 comments · May be fixed by #10
Open

Dimension: missing dunder methods #2

nstarman opened this issue Mar 10, 2025 · 8 comments · May be fixed by #10

Comments

@nstarman
Copy link

There are more dunder methods that are / might be relevant:

Rich relations:

  • __eq__
  • __ne__
  • __lt__
  • __le__
  • __gt__
  • __ge__

Binary operations:

  • __add__ (where L+L = L)
  • __sub__ (where L-L = L)
  • All the reflected operators.

Thoughts?

@lucascolley lucascolley transferred this issue from quantity-dev/dimension-api Mar 12, 2025
@lucascolley lucascolley changed the title Missing dunder methods Dimension: missing dunder methods Mar 12, 2025
@phlptp
Copy link

phlptp commented Mar 14, 2025

I am very unclear what less or greater means in context of a dimension

@nstarman
Copy link
Author

I was thinking of set containment, but yeah, looking again, it'd be confusing for users.

@andrewgsavage
Copy link

andrewgsavage commented Mar 17, 2025

I agree less than etc aren't needed. I also don't think add or sub are worth including - I don't see when you'd need to do that. I think users should only need to multiply/divide/pow and eq/ne.

@andrewgsavage andrewgsavage linked a pull request Mar 17, 2025 that will close this issue
@lucascolley lucascolley linked a pull request Mar 17, 2025 that will close this issue
@andrewgsavage
Copy link

pint's UnitsContainer has:

pint's UnitsContainer's attributes ```python3 ['__abstractmethods__', '__annotations__', '__class__', '__class_getitem__', '__contains__', '__copy__', '__delattr__', '__dir__', '__doc__', '__eq__', '__firstlineno__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getstate__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__module__', '__mul__', '__ne__', '__new__', '__orig_bases__', '__pow__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__rtruediv__', '__setattr__', '__setstate__', '__sizeof__', '__slots__', '__static_attributes__', '__str__', '__subclasshook__', '__truediv__', '_abc_impl', '_d', '_hash', '_non_int_type', '_normalize_nonfloat_value', '_one', 'add', 'copy', 'format_babel', 'get', 'items', 'keys', 'remove', 'rename', 'unit_items', 'values'] ```

from that list I think we'd want

 '__contains__',
 '__eq__',
 '__getitem__',
 '__iter__',
 '__len__',
 '__mul__',
 '__ne__',
 '__pow__',
 '__rmul__',
 '__rtruediv__',
 '__truediv__',

@andrewgsavage
Copy link

actually I'm not sure on whether the dict methods should be included '__contains__', '__getitem__', ... as a list or tuples of (dimension, exponent) pairs could be an implementation. a dict can't store strain [length]/[length]

@lucascolley
Copy link
Member

what would the semantics be for __iter__ and __len__?

@nstarman
Copy link
Author

Many pint methods are to support pint's specific structure where everything is composed and it computes via an internal state dict.

@nstarman
Copy link
Author

nstarman commented Mar 17, 2025

I personally like having the full set of arithmetic operators because then you can write elegant code where an operator callable can be applied to the dimension the same as the higher-level object.

def apply_op(op, x1, x2):
    ...
    result_dim = op(dim1, dim2)
    ...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants