Subclasses in python instance of a superclass

Is it possible to subclass in Python using an already created superclass?

I don’t know exactly how to ask a question, so let me give you an example. Suppose I have a Rectangle class, and I want to create another ColoredRectangle class. But I do not want everyone of ColoredRectanglethe same size to be his own new one Rectangle. Therefore, when I start ColoredRectangle, I will give him an already created instance Rectangle.

I.e., I want

r = Rectangle([1,2])
r_red = ColoredRectangle(r, "red")
r_blue = ColoredRectangle(r, "blue")

But now r_redthey r_blueshould be able to get all the methods and attributes of the rectangle. For example, suppose it Rectanglehas an attribute area().

r.area
2
r_red.area
2

r_redand r_blueshould "point" to the same Rectangle. I know I can do this by writing:

 class ColoredRectangle(Rectangle):

      def __init__(self, rectangle, color):
          self.color = color
          self.rectangle = rectangle

  r_red.rectangle.area

.

+4
3

- python, , getattr, , .

:

class Rectangle(object):
    def __init__(self, width, height):
        self.width = width
        self.height = height
    def area(self):
        return self.width * self.height


class ColoredRectangle(Rectangle):
    def __init__(self, rect, color):
        self.__dict__ = rect.__dict__
        self.color = color


rect = Rectangle(3, 5)
crect = ColoredRectangle(rect, color="blue")
print crect.width, crect.height, crect.color
#3 5 blue

Rectangle:

crect.width=10
print rect.width, rect.height
#10 5

, Python3, python 2.x: - Python3


getattr

- , ColoredRectangle Rectangle, :

eve = Rectangle(3, 5)
kain = ColoredRectangle(eve, color="blue")
abel = ColoredRectangle(eve, color="red")
print eve.color, kain.color, abel.color
#red red red

"-", Rectangle, , , getattr, :

class ColoredRectangle(Rectangle):
    def __init__(self, rect, color):
        self.rect = rect
        self.color = color
    def __getattr__(self,attr):
        return getattr(self.rect,attr)
eve = Rectangle(3, 5)

:

kain = ColoredRectangle(eve, color="blue")
abel = ColoredRectangle(eve, color="red")
print kain.color, abel.color
#blue red

__getattr__ __getattribute__:

getattr getattribute , getattr , . , , , , .

__getattr__, , :

kain.width=10
print eve.area(), kain.area(), abel.area()
# 15 50 15

, __setattr__:

def __setattr__(self, attr, value):
    if attr == "color":
        return super(ColoredRectangle,self).setattr(attr,value)
    raise YourFavoriteException
+2

, , , , - Rectangle. __getattr__ .

class ColoredRectangle(object):
  def __init__(self, rectangle, color):
      self.color = color
      self.rectangle = rectangle
  def __getattr__(self,attr):
       return getattr(self.rectangle,attr)
+4

. __dict__.

import copy

class Rectangle(object):
    def __init__(self, area):
        self.area = area

class ColoredRectangle(Rectangle):
    def __init__(self, rectangle, color):
        self.__dict__ = copy.deepcopy(rectangle.__dict__)
        self.color = color
0

Source: https://habr.com/ru/post/1614045/


All Articles