-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
Consider Abstract Base Classes for Quantity and Unit #3650
Comments
To be clear, I didn't suggest a _meta_class, just an abstract base class. |
By the way, if we created an ABC for Quantity-like, you can define something like: @classmethod
def __instancecheck__(cls, inst):
if hasattr(inst, 'unit') and isinstance(inst.unit, Unit):
return True
else:
return super(QuantityLike, cls).__instancecheck__(inst) That way And yeah, if you wanted to extend the concept to optionally support other unit frameworks that might work too. |
@embray - don't know what it is that I keep mixing up the Meta and Abstract Base Classes; they really have nothing to do with each other! Maybe it is just that I never had to think about Meta classes yet. Anyway, now updated the title and first entry, so that it is clear what this is about. I think for units there is a bit more of a need: I rather dislike the duck-typing I had to use in #1894. |
@mhvk Maybe it's because usually to make an ABC you use the ABCMeta metaclass--I get tonguetied around that sometimes too. |
👍 I think this would make things clearer. |
Issue #6517 reminded me that this is still useful. As @maxnoe wrote:
""" |
I think it would be easy to write an ABC with nothing on it, then add abstractmethods as we encounter them while typing. e.g. as noticed in @eerovaher's PR. |
Following discussion on how to let
Time
interact with units without relying overly on ducktyping, @embray suggested (#3648 (comment)) aQuantity
abstract base class, with which objects could register to let other code know they can be treated as quantities with a unit. ForUnit
itself, this would also be useful, in particular for things like functional units which share many but not all of the properties of normal units (see#1894 (comment)). This issue is to remind us of these thoughts.
p.s. Such a base class might also go quite a long way towards ensuring interoperability with other unit implementations (such as that by
amuse
).The text was updated successfully, but these errors were encountered: