6

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

I don't exactly know how to frame the question so let me give an example. Suppose I have a class Rectangle and I want to build another class ColoredRectangle. But I don't want each ColoredRectangle of the same dimensions to be its own new Rectangle. So when I initiate ColoredRectangle I would pass it an already instantiated Rectangle.

Ie., I want

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

But now r_red and r_blue should be able to get all rectangle methods and attributes. For example suppose Rectangle had an area() attribute.

r.area
2
r_red.area
2

r_red and r_blue should "point" to the same Rectangle. I know I could do this by writing:

 class ColoredRectangle(Rectangle):

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

But then I'd have to write

  r_red.rectangle.area

which is ugly.

Sebastian Wozny
  • 16,943
  • 7
  • 52
  • 69
Ben
  • 4,774
  • 5
  • 22
  • 26

3 Answers3

6

Inheritance

Inheritance is such a nice thing in python, and I don't think you have to resort to getattr hacks, if you want those, scroll down.

You can force the class dictionary to refer to another object:

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

These two will refer to the same Rectangle object:

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

This is an exellent talk on metaprogramming, and while it's title implies Python3 a lot of it also applies to python 2.x: David Beazley - Python3 Metaprogramming


getattr hacking

If for any reason however, you would want to have multiple ColoredRectangle refer to the same base Rectangle then these will conflict with each other:

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

If you'd like different "proxy objects", which can get attributes from the base Rectangle but not interfere with each other, you have to resort to getattr hacking, which is fun too:

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)

This will avoid the interference:

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

About __getattr__ versus __getattribute__:

A key difference between getattr and getattribute is that getattr is only invoked if the attribute wasn't found the usual ways. It's good for implementing a fallback for missing attributes, and is probably the one of two you want. source

Because only non found attributes will be handled by __getattr__ you can also partially update your proxies, which might be confusing:

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

To avoid this you can override __setattr__:

def __setattr__(self, attr, value):
    if attr == "color":
        return super(ColoredRectangle,self).setattr(attr,value)
    raise YourFavoriteException
Community
  • 1
  • 1
Sebastian Wozny
  • 16,943
  • 7
  • 52
  • 69
  • awesome! I was just looking into `getattr` and `setattr` and wondering about the class dictionary and how to use it. What's the difference between using `__dict__` and `__getattr__` and/or `__setattr__`? And thanks for the talk link. I'm always looking for good references. – Ben Nov 02 '15 at 00:20
  • It's three hours but Beazley is an amazing speaker. It's got the entire fun package: Regex, XML, `exec`, JIT code generation, import hacking, etc... – Sebastian Wozny Nov 02 '15 at 00:22
  • The `__dict__` example does not work if the `Rectangle` class defines `__slots__` or is written in C. – pppery Nov 02 '15 at 00:31
  • @SebastianWozny Am I not allowed to point out additional downsides. Not all python types have a\ `__dict__` attribute and expose **all their data** in it. In fact, the proxy `ColoredRectangle` is such a non-compliant type. – pppery Nov 02 '15 at 00:35
  • Why does forcing class dictionaries have the side effect you mention? Does the line `self.__dict__ = rect.__dict__` have the effect of setting the two dictionaries equal to each other in memory. So if you add one attribute to either dictionary you add it to both? – Ben Nov 02 '15 at 01:54
4

What you seem to be asking to do is to redirect attribute accesses to the underlying Rectangle object. The __getattr__ method can do this for you.

class ColoredRectangle(object):
  def __init__(self, rectangle, color):
      self.color = color
      self.rectangle = rectangle
  def __getattr__(self,attr):
       return getattr(self.rectangle,attr)
pppery
  • 3,731
  • 22
  • 33
  • 46
  • I think it's the best solution. – felipsmartins Nov 01 '15 at 14:45
  • Maybe you should explain what that `rectangle` class attribute is for. – PM 2Ring Nov 01 '15 at 15:31
  • The reason for the `rectangle` class attribute is to server as a default, so that the `__setattr__` magic method knows that it being called from `__init__` and the class is still being initialized. – pppery Nov 01 '15 at 16:50
  • 2
    Sure, but information like that belongs in the answer itself, not just a comment. FWIW, I would've said something like "The `rectangle` class attribute forces `__setattr__` to call the default `object.__setattr__` when the instance attributes `.color` and `.rectangle` are being set during `__init__`, when there isn't yet a `.rectangle` instance attribute to re-direct `__setattr__` calls to. And maybe you should also explicitly mention that if any new attributes are added to a `ColoredRectangle` instance they will actually get attached to the underlying `Rectangle` instance. – PM 2Ring Nov 01 '15 at 18:03
  • This behaviour seems like something I'd prefer to avoid. I shouldn't be able to change attributes of `Rectangle` from `ColoredRectangle`. Is it possible to prevent this? – Ben Nov 01 '15 at 23:34
  • @Ben delete the `__setattr__` method and the `rectangle` class attribute from the `ColoredRectangle` class entirely, as I did in my recent edit. – pppery Nov 01 '15 at 23:52
  • Haha. I was just experimenting with removing `__setattr__` myself. Looks like it works. Thanks. – Ben Nov 02 '15 at 00:21
  • @Ben the term `access` is ambiguous. I interpreted it to mean get and set, while you only meant get. – pppery Nov 02 '15 at 00:22
  • @pperry. Ok. good to know for the future. I'll edit the question. – Ben Nov 02 '15 at 00:24
0

Owerwrite all attributes by rectangle attributes. They are in __dict__ property.

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
pppery
  • 3,731
  • 22
  • 33
  • 46
Tomasz Jakub Rup
  • 10,502
  • 7
  • 48
  • 49