• livingcoder@programming.dev
    link
    fedilink
    arrow-up
    5
    ·
    1 year ago

    I need to move away from using inheritance in my Python. I’ve been using Rust exclusively for the past year and it’s definitely going to affect how I write code in other languages.

  • plistig@feddit.de
    link
    fedilink
    arrow-up
    4
    arrow-down
    1
    ·
    edit-2
    1 year ago

    Abc and Protocols are not the same, though. The protocol is met when all methods are implemented with the expected signature. Abstract base classes require you to opt in to be considered an Implementation of some interface. Both have different use cases. Protocols are much more fragile.

      • plistig@feddit.de
        link
        fedilink
        English
        arrow-up
        1
        ·
        1 year ago

        The opposite is true. If you have two interfaces that contain methods with the same name, then they have the same typing.Protocol. It is not possible to specify preconditions or contracts, as you would with abc.

  • o11c@programming.dev
    link
    fedilink
    arrow-up
    2
    arrow-down
    3
    ·
    edit-2
    1 year ago

    No. Duck types (including virtual subclasses) considered harmful; use real inheritance if your language doesn’t provide anything strictly better.

    It is incomparably convenient to be able to retroactively add “default implementations” to interface functions (consider for example how broken readinto is in Python). Some statically-typed languages let you do that without inheritance, but no dynamically-typed language can.

    This reads more as a rant against inheritance (without any explanation whatsoever) than a legitimate argument.