Summary:
I want to create a class that receives an object in init and becomes that object plus some extra functions that the class has. For example, the Flexible class:
class Flexible():
def __init__(self, obj):
self.obj = obj
def be_flexible(self):
print("Do something")
car = Flexible(Car('BMW'))
plane = Flexible(Plane('Boeing 747'))
car.drive()
car.park()
car.be_flexible()
plane.fly()
plane.be_flexible()
Details:
I have a Shape class and many Shapes that inherit from this class.
class Shape():
def __init__(self, x, y, color=(0, 0, 0)):
self.x, self.y = x, y
self.anchor_x, self.anchor_y = 0, 0
self.rotation = 0
self.color = color
class Circle(Shape):
def __init__(self, x, y, r, **kwargs):
super().__init__(x, y, **kwargs)
self.r = r
class Rectangle(Shape):
def __init__(self, x, y, w, h, **kwargs):
super().__init__(x, y, **kwargs)
self.w = w
self.h = h
class Triangle(Shape):
def __init__(self, x, y, x2, y2, x3, y3, **kwargs):
super().__init__(x, y, **kwargs)
self.x2 = x2
self.y2 = y2
self.x3 = x3
self.y3 = y3
The above code is simplified, all objects contain getters, setters, and various methods to change the shapes.
I want to add a method toggle_anchor(), to show/hide the anchor point. All shapes should be able to access this method. Normally I would add this in the class Shape, but these classes come from an external library, so I cannot modify it.
Therefore, I was hoping I could do something like this (note, AnchorShape has a Circle inside, which would be plotted to show where the shape's anchor point is):
class AnchorShape():
def __init__(self, object):
self.object = object
self.anchor_shape = Circle(self.object.anchor_x + self.object.x,
self.object.anchor_y + self.object.y, 1)
def toggle_anchor(self):
...
where the following code will work:
circle = AnchorShape(Circle(x, y, r))
print(f"Created circle (radius:{circle.r}")
square = AnchorShape(Rectangle(x, y, s, s))
print(f"Created square (side:{square.w})")
circle.toggle_anchor()
square.toggle_anchor()
I am open to other ways to do this, I just want to be able to use shapes as normal with the new functionality.
I am trying to avoid doing the following, as this involves heavy code duplication:
class AnchorCircle(Circle):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.anchor_shape = Circle(self.anchor_x + self.x,
self.anchor_y + self.y, 1)
def toggle_anchor(self):
...
class AnchorRectangle(Rectangle):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.anchor_shape = Circle(self.anchor_x + self.x,
self.anchor_y + self.y, 1)
def toggle_anchor(self):
...
class AnchorTriangle(Triangle):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.anchor_shape = Circle(self.anchor_x + self.x,
self.anchor_y + self.y, 1)
def toggle_anchor(self):
...
Edit1: correct typos and added more details.
CircleinsideAnchorRectangleandAnchorTrianglein the last part of the snippet. Are they typos? Also, I wonder why theCircleis called with two arguments, while the definition requires three positional arguments (x,y,r).CircleinsideAnchorShapesis used to plot the anchor point. So anAnchorShapewill always have aShapeand aCircleto show the anchor location. Also, I added the 3rd argument toCircle, that was a typo.class Feautre(pyglet.shapes)and then add the feature you want and inherit from this class.By the way wait until someone answers or you might end up ruining the code.