4 Replies - 3673 Views - Last Post: 02 December 2011 - 06:10 PM Rate Topic: -----

#1 pvpkiller  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 24-June 11

__str__ returned non-string (type NoneType) Why am I getting that?

Posted 02 December 2011 - 04:38 PM

class Grid():

    def __init__(self, size):
        self.size = size
        self.gridx = "X" * size
        self.gridy = "X" * size

    def __str__(self):
        i = 1
        print self.gridx
        while i < self.size:
            print self.gridy
            i = i + 1
l = Grid(5)
print l



What it should be is:
xxxxx
xxxxx
xxxxx
xxxxx
xxxxx

When I run it, i get an error "__str__ returned non-string (type NoneType)"

I am new to python, so any help is appreciated.

Is This A Good Question/Topic? 0
  • +

Replies To: __str__ returned non-string (type NoneType) Why am I getting that?

#2 Simown  Icon User is offline

  • Blue Sprat
  • member icon

Reputation: 319
  • View blog
  • Posts: 650
  • Joined: 20-May 10

Re: __str__ returned non-string (type NoneType) Why am I getting that?

Posted 02 December 2011 - 05:30 PM

__str__ should return a string representation of your object, you aren't returning anything from your __str__ method, which is why it is complaining.

If you want that grid printing out, your __str__ method, using list comprehension, could look like:

def __str__(self):
   return "\n".join(x for self.gridx in range(self.size))



Which would give you size lines of gridx, separated by newline characters. If gridx and gridy are the same value, then both are not necessary, you can achieve returning gridx, size times to achieve the same result.
Was This Post Helpful? 1
  • +
  • -

#3 pvpkiller  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 24-June 11

Re: __str__ returned non-string (type NoneType) Why am I getting that?

Posted 02 December 2011 - 05:36 PM

I want def __str__(self) to actually return the grid. When i followed your method, i got:
x
x
x
x
x

instead of:
xxxxx
xxxxx
xxxxx
xxxxx
xxxxx

What i really want in the end is for def __str__(self): to return a square grid of "X"s.
Can you help? And thank you for the reply.
Was This Post Helpful? 0
  • +
  • -

#4 pvpkiller  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 24-June 11

Re: __str__ returned non-string (type NoneType) Why am I getting that?

Posted 02 December 2011 - 05:41 PM

Hey, thanks, but I figured it out. Thanks to your help.
I did this:
return "\n".join("X" * self.size for self.gridx in range(self.size))

Was This Post Helpful? 0
  • +
  • -

#5 Simown  Icon User is offline

  • Blue Sprat
  • member icon

Reputation: 319
  • View blog
  • Posts: 650
  • Joined: 20-May 10

Re: __str__ returned non-string (type NoneType) Why am I getting that?

Posted 02 December 2011 - 06:10 PM

Ooops, slight typo on my part, it should be:

return "\n".join(self.gridx for x in range(self.size))


Rather than the other way round as I first suggested. Serves me right for not checking my code really! Of course, your way works too :)
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1