1

I wrote the following program:

def split_and_add(invoer):                              
    rij = invoer.split('=')
    rows = []
    for line in rij:
        rows.append(process_row(line))
    return rows

def process_row(line):                                  
    temp_coordinate_row = CoordinatRow()
    rij = line.split()
    for coordinate in rij:
        coor = process_coordinate(coordinate)
        temp_coordinate_row.add_coordinaterow(coor)
    return temp_coordinate_row

def process_coordinate(coordinate):
   cords = coordinate.split(',')
   return Coordinate(int(cords[0]),int(cords[1]))

bestand = file_input()
rows = split_and_add(bestand)
for row in range(0,len(rows)-1):
    rij = rows[row].weave(rows[row+1])
    print rij

With this class:

class CoordinatRow(object):

def __init__(self):
    self.coordinaterow = []

def add_coordinaterow(self, coordinate):
    self.coordinaterow.append(coordinate)

def weave(self,other):
    lijst = []
    for i in range(len(self.coordinaterow)):
        lijst.append(self.coordinaterow[i])
        try:
            lijst.append(other.coordinaterow[i])
        except IndexError:
            pass 
    self.coordinaterow = lijst
    return self.coordinaterow

However there is an error in

for row in range(0,len(rows)-1):
    rij = rows[row].weave(rows[row+1])
    print rij

The outcome of the print statement is as follows:

[<Coordinates.Coordinate object at 0x021F5630>, <Coordinates.Coordinate object at 0x021F56D0>]

It seems as if the program doesn't acces the actual object and printing it. What am i doing wrong here ?

5
  • 1
    What output were you expecting for your Coordinates.Coordinate objects? You've not defined the special method __str__() (AFAICT). Commented Dec 2, 2014 at 20:52
  • how shouldi define such a method in this case? Commented Dec 2, 2014 at 20:54
  • 1
    @Johnsyweb its actually __repr__ that he is seeing Commented Dec 2, 2014 at 20:54
  • This has to be a dup of something, but I couldn't find a question that didn't assume prior knowledge of the fact that repr and __repr__ exist in the first place… Commented Dec 2, 2014 at 20:57
  • Ah yes, of course. Thanks @JoranBeasley. My question still stands, regardless :) Commented Dec 3, 2014 at 9:58

1 Answer 1

3

This isn't an error. This is exactly what it means for Python to "access the actual object and print it". This is what the default string representation for a class looks like.

If you want to customize the string representation of your class, you do that by defining a __repr__ method. The typical way to do it is to write a method that returns something that looks like a constructor call for your class.

Since you haven't shown us the definition of Coordinate, I'll make some assumptions here:

class Coordinate(object):
    def __init__(self, x, y):
        self.x, self.y = x, y
    # your other existing methods
    def __repr__(self):
        return '{}({}, {})'.format(type(self).__name__, self.x, self.y)

If you don't define this yourself, you end up inheriting __repr__ from object, which looks something like:

return '<{} object at {:#010x}>'.format(type(self).__qualname__, id(self))

Sometimes you also want a more human-readable version of your objects. In that case, you also want to define a __str__ method:

    def __str__(self):
        return '<{}, {}>'.format(self.x, self.y)

Now:

>>> c = Coordinate(1, 2)
>>> c
Coordinate(1, 2)
>>> print(c)
<1, 2>

But notice that the __str__ of a list calls __repr__ on all of its members:

>>> cs = [c]
>>> print(cs)
[Coordinate(1, 2)]
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.