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

Magic Methods Python, Stuck on a problem

Hello, How are you doing there!

below is the code; NOTE > There are 2 modules, Module 1 has the class Item and module 2 has the class Inventory

MODULE NUMBER 1

class Item:
    def __init__(self, name, description):
        self.name = name
        self.description = description

    def __str__(self):
        return '{}: {}'.format(self.name, self.description)

MODULE NUMBER 2

class Inventory:
    def __init__(self):
        self.slots = []

    def add(self,item):
        self.slots.append(item)

    def __str__(self):
        return str(self.slots)

    def __len__(self):
        return len(self.slots)

PYTHON SHELL

from items import Item
from inventory import Inventory

>>> coin = Item("Coin","Common Currency used for buying goods")

>>> inventory = Inventory()

>>> inventory.add(coin)

>>> print(inventory)

#output    [<items.Item object at 0x7efdb82726>]

>>> inventory.add("Chocolate bar")

>>> print(inventory)

#output  [<items.Item object at 0x7efdb82726>, 'Chocolate bar']

Alright, I suppose you already figured out what my problem is, If not, that's cool too.

The problem is that the output is not according to what I expect. I am expecting the "coin" name and description to be in it, not <items.Item object at 0x7efdb82726>, Is there a way to simplify it to a more readable version at least, if not a complete solution?

I would LOVE and appreciate your help. THXSSSSSS

2 Answers

Steven Parker
Steven Parker
243,201 Points

Your existing method works if you directly print the item (like "print(inventory.slots[0])").

But there's a different magic method you can add to "Item" that provides a string representation of an object:

    def __repr__(self):
        return self.__str__()  # just call the existing string conversion method

Hey Steven! thanks for the answer, it solved my problem but not my understanding of how did it work.

Like, I can't figure out what is the process going on here, could you please de-construct the magic method you specified here?

Steven Parker
Steven Parker
243,201 Points

You'd already done the job of providing a string conversion method, which worked fine if you print the item itself. But when you print the whole list instead, the system doesn't use your method. But if it exists, the "representation" method is used to determine how to show the individual items.

Another solution would be to leave the "item" code alone, but change the string conversion for "Inventory" to do individual string conversions on the items in the slots:

    def __str__(self):
        return "[" + ", ".join([str(item) for item in self.slots]) + "]"

Cool job Steven LOVED your answers, TeamTreeHouse should put a system in place for upvoting comments and even providing a "Best Comment" option, cause I see a lot of good answers and explainings in comments.

Have a nice day!!! :)