2

I am attempting to combine two classes into one class. Towards the end of the code block you will see a class called starwarsbox. This incorporates the character and box classes. The goal is print out a box made out of asterisks and the information of a Star Wars character (this is for my learning). I have tried looking up how to use repr but have had no luck implementing it. I appreciate your help.

I get <__main__.starwarsbox object at 0x000000000352A128>

class character:
    'common base class for all star wars characters'
    charCount = 0

    def __init__(self, name, occupation, affiliation, species):
        self.name = name
        self.occupation = occupation
        self.affiliation = affiliation
        self.species = species
        character.charCount +=1

    def displayCount(self):
        print ("Total characters: %d" % character.charCount)

    def displayCharacter(self):
        print ('Name :', self.name, ', Occupation:', self.occupation, ', Affiliation:', self.affiliation, ', Species:', self.species)


darth_vader = character('Darth Vader', 'Sith Lord', 'Sith', 'Human')
chewbacca = character('Chewbacca', 'Co-pilot and first mate on Millenium Falcon', 'Galactic Republic & Rebel Alliance', 'Wookiee')


class box:
    """let's print a box bro"""

    def __init__(self, x, y, title):
        self.x = x
        self.y = y
        self.title = title

    def createbox(self):
        for i in range(self.x):
            for j in range(self.y):
                 print('*' if i in [0, self.x-1] or j in [0, self.y-1] else ' ', end='')
            print()

vaderbox = box(10, 10, 'box')
vaderbox.createbox()


class starwarsbox(character, box):
    def __init__(self, name, occupation, affiliation, species, x, y, title):
        character.__init__(self, name, occupation, affiliation, species)
        box.__init__(self, x, y, title)

    def __str__(self):
        return box.__str__(self) + character.__str__(self)

newbox = starwarsbox('luke','jedi','republic','human',10,10,'box')

print(repr(newbox))
4
  • 1
    repr invokes __repr__, not __str__. Commented Feb 11, 2016 at 17:46
  • what the would call be then? I tried print (str(newbox)) and it still shows <__main__.starwarsbox object at 0x000000000350A128> Commented Feb 11, 2016 at 17:50
  • Implement your __str__ function in box and Character classes then use print(newbox). Commented Feb 11, 2016 at 17:58
  • str attempts to invoke __str__ and falls back to __repr__ if it's not defined. (That is to say, object.__str__ delegates to object.__repr__.) repr always calls __repr__. Commented Feb 11, 2016 at 17:58

1 Answer 1

1

First, as chepner mentions, the last line should be print(str(newbox)).

starwarsbox has __str__ implemented, but box and character don't.

box should look like:

    def __str__(self):
        result = ""
        for i in range(self.x):
            for j in range(self.y):
                result += '*' if i in [0, self.x - 1] or j in [0, self.y - 1] else ' '
            result += '\n'
        return result

and character should look like:

    def __str__(self):
        return 'Name :' + self.name + ', Occupation:' + self.occupation + ', Affiliation:' + self.affiliation + ', Species:' + self.species

Compare these to your code, and see how you could implement displayCharacter and createBox using the implementations of __str__. :)

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

5 Comments

Thank you so much!!! This worked! :-). I am so grateful. I've been trying to wrap my head around this for two days... lol.
What is the difference between print(str(newbox)) and print(newbox).
I just tried doing print(newbox) and it works the same as print(str(newbox)). I'm guessing because the other changes made ensure that it is a string being printed.
@WoLy, they are functionally the same, but I thought that the direct replacement of repr with str would be more clear for asker.
it is better to explain.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.