0

I am getting this error each time I try to run this app in its present state.

TypeError: cannot concatenate 'str' and 'GameData' objects

I am trying to get a data object from my Game(GameData) class onto the browser with my html. It's a subclass of GameData() which is a template class.

class GameData(object): #This class is the template for the data for each game.
    def __init__(self):
        self.title = ''
        self.genre = ''
        self.description = ''
        self.developer = ''
        self.rating = ''
        self.image = ''

class Game(GameData): #Thas class holds all the data for each game.
    def __init__(self):
        #object for Castlevania
        self.castlevania = GameData()
        self.castlevania.title = 'Castlevania'
        self.castlevania.genre = 'Action Platformer'
        self.castlevania.description = 'Released in 1986 in Japan, Castlevania for the NES and Famicom Disc System spawned a series rich in action as well as exploration. This first game was merely a simple platformer but it inspired many changes to the formula over the years and invented the "Metroidvania" genre.'
        self.castlevania.developer = 'Konami'
        self.castlevania.rating = '7.5/10'
        self.castlevania.image = 'images/t_castlevania.png'

There are other objects but if I can get one of them to work I can figure out the rest. I need it to get into this elif statement highlighted with a comment.

class MainHandler(webapp2.RequestHandler):
    def get(self):
        i = Intro()
        d = GameData()
        g = Game()

        if self.request.GET:
            page = self.request.GET['page']
            if page == 'main_page':
                self.response.write(i.head + i.main_page + i.main_title + i.main_links)

            elif page == 'castlevania': #The data needs to go in this concatenation.
                self.response.write(i.head + i.main_page + i.castlevania + (the data object should go here) + i.main_links)

From there I know what to do. I just need to know how to convert the data into a string so I can concatenate it. If anyone can help I would appreciate it. Also I tried using an array for the objects but that didn't work for me either.

martineau
  • 119,623
  • 25
  • 170
  • 301
Matt Lee
  • 521
  • 2
  • 5
  • 16

1 Answers1

0

You just need to include the __str__ function in your Game(GameData) class.

eg.

def __str__(self):
    # Here you would put whatever logic in terms of returning a string
    # representation of your game object. Eg:
    return self.castlevania.title

Then, you would simply call str(g) where g is your Game(GameData) object

Apoorv Kansal
  • 3,210
  • 6
  • 27
  • 37
  • Do a return line for each part of the object? Because that's a lot more code to add. – Matt Lee Dec 17 '16 at 01:18
  • I have like 4 more objects. – Matt Lee Dec 17 '16 at 01:18
  • What exactly do you want to return? Can you state specifically what should go into `(the data object should go here)`? – Apoorv Kansal Dec 17 '16 at 01:27
  • This is the common and advised way of representing python objects as strings. – Apoorv Kansal Dec 17 '16 at 01:28
  • If that's what I have to do that's okay. I just wanted to know if there was a way with less code. But thanks. I appreciate the info. – Matt Lee Dec 17 '16 at 01:45
  • @Matt Lee: The idea behind representing python objects as strings, which is usually done by implementing an `__repr__()` method, is that it should return a string that represents calling the object's class constructor (its `__init__()` method, and pass it the arguments needed to recreate the object and its contents: i.e. `"GameData(arg1, arg2, whatever)"`. An alternative approach would be to use the `pickle` module. See the question [_Saving an Object (Data persistence in Python)_](http://stackoverflow.com/questions/4529815/saving-an-object-data-persistence-in-python). – martineau Dec 17 '16 at 03:38