[Python] Add an instance to a virtual subclass?

Discussão em 'Python' iniciado por Stack, Novembro 24, 2025.

  1. Stack

    Stack Membro Participativo

    With abstract subclasses on can use register to add other classes as a "virtual" subclass, what would be a way to do that for a single instance based on it fullfilling some criteria?

    The "sensible" alternative I came up with is using virtual subclassing but changing the instance check dunder method

    class Controller:
    def __init__(self,smart)
    pass

    class SmartController(ABC):
    def __instancecheck__(self)
    return self.smart



    but this didn't work since Controllers metaclass actually has to have an altered instance_check for this to work, which makes me think you might have to do something with metaclasses, but if Controller allready has some other metaclass I don't know if it would work...

    possible reason for doing this: Controller might have some childclasses which the individual instances can either be "smart" or "stupid"

    I realize this might be a very bad thing to do but if there is some "most sensible" way to do it then I would like to know how

    Also would like to know why this is actually a bad idea if it is one

    Continue reading...

Compartilhe esta Página