1

Im attempting to call the grandparent method getColor() from the Class below.

This is the grandparent class in its own file:

class IsVisible(object):
    def __init__(self):
        pass
    white = (255,255,255)
    black = (0,0,0)
    red = (255,0,0)
    blue = (0,0,255)
    green = (0,255,0)

def getColor(self):
        return self.color

This is the parent class and child class.

from IsVisible import *
class Object(IsVisible):
    def __init__(self):
        super(Object, self).__init__() 
        self.posSize = []
    def getPosSize(self):
        return self.posSize

class Rectangle(Object):
    def __init__(self):
        super(Rectangle, self).__init__()
        self.color = self.blue
        self.posSize = [20,50,100,100]

So Im trying to call getColor by creating an object

rectangle = Rectangle()

and then calling

rectangle.getColor()

However I'm getting an error. Namely:

AttributeError: 'Rectangle' object has no attribute 'getColor'

Now I have no idea how to solve this. I have tried to google "python call grandparent method" but I only get instructions for how to call a method that is overrun... I believe I have stated the inheritance correctly so I don't know what the problem is. Could anyone help me?

idjaw
  • 25,487
  • 7
  • 64
  • 83
  • 6
    In the code you showed, `getColor` is not indented under `IsVisible`. Is it in your real code? It should be. **Also** Please change `Object` to another name --- too much risk of confusion with the builtin `object`. **And,** what Python version? – cxw Oct 14 '16 at 20:29
  • 1
    PS welcome to the site! Thanks for including detail in this question --- many forget to do so. Check out the [tour](https://stackoverflow.com/tour) for more about asking questions that will attract quality answers. You also get a badge for reading the whole tour. :) – cxw Oct 14 '16 at 20:31

1 Answers1

1

If it's just indentation (and nothing else is jumping out at me) —

class IsVisible(object):
    def __init__(self):
        pass
    white = (255,255,255)
    black = (0,0,0)
    red = (255,0,0)
    blue = (0,0,255)
    green = (0,255,0)

    def getColor(self):    # <--- indent this
        return self.color

But be careful about class vs. instance variables. See this answer for more (not copied here since it's not the answer to your question above).

Also, please don't use Object as the name of a class :) . It is too likely to lead to confusion with the real object, or to confuse the reader about what you mean. How about ColoredShape instead of IsVisible, PositionedShape instead of Object, and Rectangle(PositionedShape)?

Community
  • 1
  • 1
cxw
  • 16,685
  • 2
  • 45
  • 81
  • Oh, I see! Thank you very much for spotting it! And thanks for the suggestion of name change. I was uncertain what to name it before. – Slibbfalusken Oct 14 '16 at 23:45
  • @Slibbfalusken Good news --- happy to help! If this answer solved your problem, would you please hit the checkmark to accept this answer? That will clear your question off the "unanswered" lists --- and we will both get reputation points :) . ([tour](https://stackoverflow.com/tour)) – cxw Oct 17 '16 at 16:51