In a particular program I had written, I noticed I had a few classes with this pattern:
class IdObject: '''Objects with generated id properties''' def __init__(self, id_generator): self.id_generator = id_generator self.id = id_generator() class Node(IdObject): '''Represents a node in the graph''' def __init__(self, id_generator): super().__init__(id_generator)
That is, there was particular property that I wanted a class to have, so I made a class that instantiated that property, and then just subclassed from that other class.
However, I realized I could do this for literally every other property. Having been a programmer for a while, doing that strikes me as just wrong, but it would be helpful for some more experienced and knowledgable programmers to help me discover exactly why doing that would be wrong.
For another example, it doesn't seem right to do:
class HasLength: def __init__(self, length): self.length = length class HasWidth: def __init__(self, width): self.width = width class Rectangle(HasLength, HasWidth): def __init__(self, length, width): HasLength.__init__(self, length) HasWidth.__init__(self, width)
This very much reminds me of Java's interfaces, but with properties.
So is it bad form to create a new class for each new property? Why or why not?