[Python] How come an abstract base class in python can be instantiated?

Discussão em 'Python' iniciado por Stack, Dezembro 18, 2024.

  1. Stack

    Stack Membro Participativo

    It is very surprising to me that I can instantiate an abstract class in python:

    from abc import ABC

    class Duck(ABC):
    def __init__(self, name):
    self.name = name

    if __name__=="__main__":
    d = Duck("Bob")
    print(d.name)


    The above code compiles just fine and prints out the expected result. Doesn't this sort of defeat the purpose of having an ABC?

    Continue reading...

Compartilhe esta Página