1

I am developing a module that draws lines and finds their midpoints. For purposes of testing, I want to create some string outputs from the relevant classes.

class Line:
  def __init__(self, endpoints):
    self.start = endpoints[0]
    self.end = endpoints[1]

  def midpoint():
    x = (start.getX + end.getX) / 2.0
    y = (start.getY + end.getY) / 2.0
    return Point(x, y)

  def __str__(self):
    return "line from " + `self.start` + " to " + `self.end` + "."

class Point:
  def __init__(self, x, y):
    self.x = x
    self.y = y

  def getX():
    return x

  def getY():
    return y

  def __str__(self):
    return "[" + str(self.x) + ", " + str(self.y) + "]"

  __repr__ = __str__

point1 = Point(4,5)
point2 = Point(0,0)
line1 = Line([point1, point2])

print line1
print line1.midpoint

Expected output:

line from [4, 5] to [0, 0]
[2.0, 2.5]

Instead I get:

line from [4, 5] to [0, 0]
<bound method Line.midpoint of <__main__.Line instance of 0x105064e18>>

How can I get the expected string representation of the midpoint, which is being returned as an instance of the Point class?

8
  • 1
    You don't call line1.midpoint, you're just referencing it. BTW, I thought that noone used the backticks in Python code anymore. Commented Dec 6, 2016 at 16:31
  • 1
    either call your midpoint (add brackets d'oh) or make it a property... Commented Dec 6, 2016 at 16:32
  • 1
    The backticks call repr so it would be repr(self.start) or you could use the str(self.start). FYI: The backticks are removed in Python 3. Commented Dec 6, 2016 at 16:37
  • 1
    @pgblu You can use repr(self.start) instead of the backticks or use: return 'line from {!r} to {!r}'.format(self.start, self.end) Commented Dec 6, 2016 at 16:37
  • 1
    Apart from backticks being removed as @Matthias already indicated, print as a statement is gone in Python 3 as well. Start adding from __future__ import print_function to the top of your code and use print() Commented Dec 6, 2016 at 16:39

2 Answers 2

3

You are printing the method itself, not the returned value of the method. Change your last line to this:

print line1.midpoint()

Also, the first definition line of your method should use self as the only parameter, like so:

def midpoint(self):

The same applies to the rest of the methods, they should have self as a parameter (Point.getX and Point.getY).

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

4 Comments

I've tried that. But when I add the parentheses, I get a TypeError: midpoint() takes no arguments (1 given)
@pgblu You need to add self as a parameter. There are also several other errors I see in your code. I suggest you go through it and clean it up.
But wouldn't print line1.midpoint() call repr on the returned value? In that case we would need to use print str(line1.midpoint()).
@Matthias Your correct. I belive the OP is confused about what __repr__ and what __str__ are for. They're used to give a visual representation of a given object.
1

In the midpoint method, you should have start.getX(), start.getY(), end.getX(), and end.getY() should have "self." in front of it. You should also have "self" as a parameter for the method for every method in a class.

midpoint method

I will paste the entire code below to show you exactly what I have done.

entire code

Comments

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.