python "'y' object has no attribute 'x'" when calling a grandparent method x
<p>Im attempting to call the grandparent method getColor() from the Class below.</p>

<p>This is the grandparent class in its own file:</p>

<pre><code>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
</code></pre>

<p>This is the parent class and child class.</p>

<pre><code>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]
</code></pre>

<p>So Im trying to call <code>getColor</code> by creating an object </p>

<pre><code>rectangle = Rectangle()
</code></pre>

<p>and then calling </p>

<pre><code>rectangle.getColor()
</code></pre>

<p>However I'm getting an error. Namely:</p>

<pre><code>AttributeError: 'Rectangle' object has no attribute 'getColor'
</code></pre>

<p>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?</p>

<p>If it's just indentation (and nothing else is jumping out at me) &mdash;</p>

<pre><code>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):    # &lt;--- indent this
        return self.color
</code></pre>

<p>But be careful about class vs. instance variables.  See <a href="http://stackoverflow.com/a/3434596/2877364">this answer</a> for more (not copied here since it's not the answer to your question above).  </p>

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

python class inheritance pygame