Python abstract classes - how to discourage instantiation?

后端 未结 5 1458
面向向阳花
面向向阳花 2021-02-07 04:58

I come from a C# background where the language has some built in \"protect the developer\" features. I understand that Python takes the \"we\'re all adults here\" approach and

5条回答
  •  囚心锁ツ
    2021-02-07 05:17

    Create your 'abstract' class and raise NotImplementedError() in the abstract methods.

    It won't stop people using the class and, in true duck-typing fashion, it will let you know if you neglect to implement the abstract method.

提交回复
热议问题