Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pythonic way to name derived classes

I can't figure out what is the more acceptable naming convention for derived classes. Let's say we have a base class Fruit, from which we derive, classes for apple, orange and passion fruit. It is a concensus that the name of the base class should appear in the derived ones, but should it appear in the beginning of the class name or at the end?

Which is more Pythonic: FruitApple, FruitOrange and FruitPassionFruit or AppleFruit, OrangeFruit and PassionFruitFruit?

like image 793
Boris Gorelik Avatar asked May 24 '26 09:05

Boris Gorelik


1 Answers

Use common sense.

In your example, as others have discussed, the Fruit part is obviously redundant. Use just Apple, Orange, etc. Let's consider use another example instead.

If I had a base class called Plugin, I would name the derived class FormattingPlugin, which I read as “plugin that does formatting”. The alternative, PluginFormatting, reads as “formatting of plugins”. I guess the reading is subjective, so use your own best judgment. But don't name it just Formatting, because, unlike in the fruits example, that's not a complete enough description of what the class does.

like image 196
Petr Viktorin Avatar answered May 25 '26 23:05

Petr Viktorin