I would like to make a class composition so that the instance variables of the composing classes become instance variables of the composition but with adjusted names.
The application for this is in defining new objects for drawing in matplotlib. One example is that I would like to have a function drawMyArrow
that draws an arrow with possibly different colors (and other specifications) for its head, tail, and arc. I would like to be able to pass various specifications for the head, tail, and arc via keyword arguments in drawMyArrow
. I haven't worked with classes before, but reading up on this online, I believe that the best way to solve my problem is to define a class MyArrow
that is a composition of some classes ArrowHead
and ArrowArc
.
To illustrate my problem, consider a simple toy example. Let's define a class Room
that is a composition of the classes wall
, window
, and door
.
class Door:
def __init__(self, color='white', height=2.3, width=1.0):
self.color = color
self.height = height
self.width = width
class Window:
def __init__(self, color='white', height=1.0, width=0.8):
self.color = color
self.height = height
self.width = width
class Wall:
def __init__(self, color='white', height=2.5, width=4.0):
self.color = color
self.height = height
self.width = width
class Room:
def __init__(self):
self.door = Door()
self.window = Window()
self.wall = Wall()
The instance variables of Door
, Window
, and Wall
are color
, height
, width
. I would like Room
to have instance variables doorcolor
, windowcolor
, wallcolor
, doorheight
, windowheight
, etc. I could add all nine instance variables to Room
explicitly and define set
and get
functions for them. But if I later decide to add more instance variables to Door
, Window
, or Wall
I would always need to edit the code for Room
again too. Is there a way to code Room
so that it adopts (and renames) the instance variables from its component classes automatically?
You are using composition - no need to replicate accessors for your members. You can easily access the attributes through your composed members:
You might profit from a base class for your "parts" and a changed
__init__(..)
for yourRoom
class though:Create objects and print them:
Output: