Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Object-Oriented Python (retired) Inheritance __str__

Lior Dolinski
Lior Dolinski
3,905 Points

__str__ simply won't work when i print my instance

iv'e added the str method on to my monster class, but whenever i create an instance and try to print it, it just gives me the general <monster.Monster object at *memory space*>

monster.py:

from combat import Combat
import random


COLORS = ['yellow', 'blue', 'green', 'red']


class Monster(Combat):
    min_hit_points = 1
    max_hit_points = 1
    min_experience = 1
    max_experience = 1
    sound = 'roar'

    def __init__(self, **kwargs):
        self.hit_points = random.randint(self.min_hit_points,self.max_hit_points)
        self.exeprience = random.randint(self.min_experience,self.max_experience)
        self.color = random.choice(COLORS)

        for key, value in kwargs.items():
            setattr(self, key, value)

    def battlecry(self):
        return self.sound.upper()

    def __str__(self):
        return '{} {}, HP: {} , XP: {}'.format(self.color.title(),
                                               self.__class__.__name__,
                                               self.hit_points,
                                               self.exeprience)


class Troll(Monster):
    min_hit_points = 3
    max_hit_points = 5
    min_experience = 2
    max_experience = 6
    sound = 'growl'


class Dragon(Monster):
    min_hit_points = 5
    max_hit_points = 10
    min_experience = 6
    max_experience = 10
    sound = 'raaaaaaaaar'



class Goblin(Monster):
    max_hit_points = 3
    max_experience = 2
    sound = 'squeak'

what i type in console:

juju = Monster() #creating the instance print(juju)

thanks!

Anish Walawalkar
Anish Walawalkar
8,534 Points

Can you post the code for the combat module. Could you also elaborate on what you mean by " it just gives me the general"

2 Answers

Lior Dolinski
Lior Dolinski
3,905 Points

Anish Walawalkar oh i'm sorry it probably got left out when i posted.

by general i mean the

<monster.Monster object at 0x03101990>

and here is my combay.py

import random


class Combat:
    def __init__(self):
        dodge_limit = 6
        attack_limit = 6

    def dodge(self):
        roll = random.randint(1, self.dodge_limit)
        return roll > 4

    def attack(self):
        roll = random.randint(1, self.attack_limit)
        return roll > 4

Hi Lior, I have tested your code in IDE and it works perfectly for me. Maybe you should check again. Have you saved your code before running it?

Lior Dolinski
Lior Dolinski
3,905 Points

I sure have.. What IDE do you use? perhaps it's a specific problem with my PyCharm..

Thanks again!

Have you also tried your code in workspaces? Workspaces uses python 3.5.0 which is the version I am currently using