Practicing Inheritance in Python: Why AttributeError, despite my class and attribute seem setup correctly?
<p>I've got the following file/folder setup for my Python project (practicing inheritance) as follows:</p>

<pre><code>my_project/new_classes.py
my_project/human/Human.py
my_project/human/__init__.py (note, this is a blank file)
</code></pre>

<p>Although it appears that my <code>class Samurai(Human)</code> is setup correctly, I'm receiving the following error:</p>

<pre><code>line 41, in &lt;module&gt;
tom.sacrifice()
AttributeError: 'Samurai' object has no attribute 'sacrifice'
</code></pre>

<p>Here's the code from my <code>my_project/new_classes.py</code> file:</p>

<pre><code>from human.Human import Human
class Wizard(Human):
    def __init__(self):
        super(Wizard, self).__init__()   
        self.intelligence = 10           
    def heal(self):
        self.health += 10
class Ninja(Human):
    def __init__(self):
        super(Ninja, self).__init__()    
        self.stealth = 10                
    def steal(self):
        self.stealth += 5
class Samurai(Human):
    def __init__(self):
        super(Samurai, self).__init__()  
        self.strength = 10               
    def sacrifice(self):
        self.health -= 5
# create new instance of Wizard, Ninja, and Samurai
harry = Wizard()
rain = Ninja()
tom = Samurai()
print harry.health
print rain.health
print tom.health
harry.heal()
print harry.health
rain.steal()
print rain.stealth
tom.sacrifice()
print tom.health
print tom.stealth
</code></pre>

<p>The code breaks when it gets to line <code>tom.sacrifice()</code> - any ideas why?</p>

<p>One other question I had was at first, when trying to import the parent class, I was using the statement <code>from human import Human</code>, which I thought would work (as I thought the formula was from <code>module_directory import ModuleFileName</code>, but was receiving an error as follows: <code>TypeError: Error when calling the metaclass bases module.__init__() takes at most 2 arguments (3 given)</code>. I resolved this by changing my import statement to, <code>from human.Human import Human</code> and was wondering <em>why</em> this worked while the other did not? I may be confused about correctly importing classes and was hoping someone also might be able to help clarify.</p>

<p>[EDIT] Removed comments.</p>

<p>If you initialize the class in the same document, this code below runs. The reason you are receiving the error is because you are trying to change a variable that was never set during initialization.</p>

<pre><code>class Human(object):
    def __init__(self):
        self.health = 100
        self.stealth = 0
        self.strength = 15
</code></pre>

<p>You have to initialize the variables. Make sure that every <code>Human</code> has the properties that you would want to change. You can probably set <code>=</code> the health, but not change <code>-=5</code> it if it was not initialized.</p>

python python-2.7 inheritance