-
-
Notifications
You must be signed in to change notification settings - Fork 8.2k
Inheritance for native types is not supported #1159
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
Comments
When UART timeout of zero is given, make read() return data already a…
I don't understand how this never got priority since 2015. |
Not really, there are basic tricks which minimize boilerplate to the point you can't really speak of duplication anymore. For example:
|
@stinos I'm not providing python code. I'm providing an extension to micropython by adding a module in C. |
The same principle can also be applied in C if you're not using a const dict. |
@stinos Please explain how to achieve this when adding a module. I tried setting |
I don't have an example ready but thinking of it, doing this in C is actually a bit different but less bloated as you don't need storage for each method. The idea is: you implement the type's attr function and when MicorPython asks you for an attribute you look it up in your class first, and if not found you look in your 'base' class instead and return it if found. Here's some untested pseudocode thrown together:
|
@stinos Thanks for your example, I'll give it a try. |
Apologies if I'm missing the point here but it seems to me that providing generalised inheritance of built in types is considerably more complex than copying attributes. Consider properties like mutability, the ability to support item assignment, the ability to iterate over the instance, support for the buffer protocol and doubtless more. These may or may not be present in the base class. |
@stinos @peterhinch It's worth noting that at least in Python this is already covered. (PEP253, an example) |
Yes of course, that's also why this issue exists.
You're not missing any point, the example shown isn't generalised at all.
Well I surely hope there are shortcuts not shown in those example then, instead of having to copy that 40-line type definition :P |
See #4360 for a solution to this issue. It just does single inheritance but I think that is enough. |
Default load_attr implementation (as used for all native types) doesn't take type inheritance (->bases_tuple) into account. I'm working on OrderedDict implementation, and it inherits most of its methods from dict, but adds few (well, 1) new method. Of course, this new method can be ignored, and old trick of putting the same .locals_dict to both types can be used. But I wanted to look into making truly inheritable methods, which requires setting custom .load_attr type method. And we already have implementation of this method which takes inheritance into account - for user classes/instances. But trying to use it, it assumes that object whose attr is being looked up is instance, so making it work with native objects means adding even more conditions to existing bunch of those (and which are already pretty mind-boggling). Another alternative is to make clean, "optimized" inheritance-friendly .load_attr for native types, but that means code duplication (and refactoring of existing code paths, factoring out functions, which means more stack usage, etc.)
Any thoughts?
The text was updated successfully, but these errors were encountered: