How define constructor implementation for an Abstract Class in Python?

前端 未结 5 1166
难免孤独
难免孤独 2020-12-29 01:32

I am trying to declare an abstract class A with a constructor with a default behavior: all subclasses must initialize a member self.n:



        
5条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-29 02:00

    You can implement something like below :

    from abc import ABC
    class A(ABC):
        def __init__(self, n):
            self.n = n
            super(A,self).__init__()
    

    Instantiating this class would throw an error

提交回复
热议问题