Question

In simplest terms what I want is a tuple with one or two additional methods. __new__ or __init__ are not going to be modified.

I would like to create an abstract base class that is subclass of collections.abc.Sequence. Then I want to use it for what is basically a tuple subclass. The class diagram is something like:

   collections.abc.Sequence
          /       \
         /         \
        /           \
      MyABC       tuple
        \           /
         \         /
          \       /
           MyClass

Reasons:

  • MyABC defines some custom interfaces. It is there so third parties are not forced to subclass MyClass.
  • tuple is needed for its performance and the already implemented methods.

Questions:

  • Is the idiomatic way just to write class MyClass(MyABC, tuple) or should I play with the registers?
  • Are there any other obvious problems that I am missing?
  • Will the space and performance benefits of tuple be lost because of the subclassing?
Was it helpful?

Solution

  • Is the idiomatic way just to write class MyClass(MyABC, tuple) or should I play with the registers?

    It looks to me like a quite idiomatic way of doing this. This is also what is called an "inheritance diamond". The __init__ method for MyClass could look like this.

  • Are there any other obvious problems that I am missing?

    Well, it depends on some aspects which are not quite specified in the question, like what do you exactly want to do? How are you doing it? How is the usage pattern of this new subclass? How general/specific are the new methods you have in your new class? There is a more efficient/cleaner/readable/easier/whatever way of doing this without creating a subclass? And even if yes, is it still worth doing, if you will gain more flexibility/readability and so?

    As a general rule, I would say that it's not only ok, but also desirable to create subclasses if you will use it often enough and/or it will enable you to solve some problem more easily without drastic/highly undesirable collateral effects.

  • Will the space and performance benefits of tuple be lost because of the subclassing?

    That's a tricky question. Again, depending on what you are doing and how you design your subclass etc..., possibly. Remember, for example, that tuples are an immutable type and this property has some performance advantages over mutable types. So, if you e.g. would somehow modify this property, this could represent some loss, compared to the original tuple. I'm also not quite sure how Python will see a subclass of a tuple. By that I mean, I don't know if Python would still 'blindly believe' your class is immutable and so still behave exactly like a tuple. And if not, how bad it would actually be in your specific case. The immutable/mutable issue is simply an example, other even more relevant issues might exist, but I couldn't really say for sure... Maybe you could do some small-scale test to measure the performance.

Considering some of these aspects (By Guido himself) could also be a good idea.

I hope this helps!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top