1

I've got the following file/folder setup for my Python project (practicing inheritance) as follows:

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

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

line 41, in <module> tom.sacrifice() AttributeError: 'Samurai' object has no attribute 'sacrifice' 

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

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 

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

One other question I had was at first, when trying to import the parent class, I was using the statement from human import Human, which I thought would work (as I thought the formula was from module_directory import ModuleFileName, but was receiving an error as follows: TypeError: Error when calling the metaclass bases module.__init__() takes at most 2 arguments (3 given). I resolved this by changing my import statement to, from human.Human import Human and was wondering why 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.

[EDIT] Removed comments.

5
  • 1
    What is the definition of Human? Is it a new-style class? Commented Sep 18, 2016 at 18:04
  • 1
    Are you sure you cannot trim it down to minimal reproducible example? Commented Sep 18, 2016 at 18:08
  • Can you provide the human class so we can run it? Commented Sep 18, 2016 at 18:11
  • Following wha Lukasz Rogalski said, your code will be easier to read and will get more/better replies if you delete redundant/off topic code comments such as '# instances of the Wizard class can perform the heal method' and if you delete all references to the ninja class. We only need the wizard & samurai examples to show that you have accessed the health property successfully the first but not second time. Commented Sep 18, 2016 at 18:18
  • I wanted to keep the full code example because there's a lot going on - Lucas provided the exact answer I was looking for. Additionally, all of the other methods were taking, while Samurai was not, thus the other methods were kept in the code example so the successful methods that were working could be observed in comparison to the method that was broken (which had spacing issues, now resolved) Commented Sep 18, 2016 at 21:11

3 Answers 3

1

I resolved this by changing my import statement to, from human.Human import Human and was wondering why this worked while the other did not?

This is because the first "Human" is your Python module (Human.py). The Human class is inside it, so that's why your import statement must be as you wrote last. You do not want to import the module, but the class.

As to your AttributeError problem, it's odd as your classes seem OK. It's probably something unusual inside the Human class. Can we have the code in it?

EDIT

I see you found the solution for the AttributeError problem so I'll leave the answer for the second problem only.

Sign up to request clarification or add additional context in comments.

2 Comments

This is exactly what I was wondering -- thank you greatly for taking the time to explain it. Just to summarize, from human.Human grabs Human.py from /human - the last commands, import Human finally imports the class Human inside of the module ... is this correct in my understanding? Thanks greatly for taking a look, and it definitely ended up being a spacing issue! Note to self: always check the tab-formatting / space-formatting on any code that's been copied and pasted as Python is tab specific! Thanks Lucas!
That's 100% correct. human is the directory, the first Human is the file and the second Human is finally the class you want to import. Glad I could help :)
0

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.

class Human(object): def __init__(self): self.health = 100 self.stealth = 0 self.strength = 15 

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

1 Comment

I believe the from human.Human import Human should import the Human class and the init method should run! Thank you for your answer and help!
0

I found my solution, in my Samurai method, I had a spacing issue!

I reformatted my code with proper tabs and everything is working!

Correct code is as follows:

class Samurai(Human): def __init__(self): super(Samurai, self).__init__() # use super to call the Human __init__ method self.strength = 10 # every Samurai starts off with 10 strength def sacrifice(self): self.health -= 5 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.