Python abstract classes - how to discourage instantiation?

后端 未结 5 1467
面向向阳花
面向向阳花 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:40

    I just name my abstract classes with the prefix 'Abstract'. E.g. AbstractDevice, AbstractPacket, etc.

    It's about as easy and to the point as it comes. If others choose to go ahead and instantiate and/or use a class that starts with the word 'Abstract', then they either know what they're doing or there was no hope for them anyway.

    Naming it thus, also serves as a reminder to myself not to go nuts with deep abstraction hierarchies, because putting 'Abstract' on the front of a whole lot of classes feels stupid too.

提交回复
热议问题