1

I'm trying to get my code to output the results of really any code to my terminal, but I can't seem to find out why it won't print. I'm just learning to code so I've been finding a lot of explanation on this site kind of confusing, so apologies if this has been asked before. This is my python file python.py:

class point(object):

    def __init__(self, x, y):

        self.x = float(x);
        self.y = float(y);

    def __str__(self):

        return("(" + self.x + "," + self.y + ")")


def main():

    first = point(2,3)


if __name__ == '__main__':
    main()

and in the terminal I'm just typing "python python.py"

2
  • 1
    just put use print. i.e. to print hi put print "hi" Commented Sep 28, 2017 at 16:29
  • The terminal in mac Commented Sep 28, 2017 at 16:29

2 Answers 2

1

Add a print statement in the main() function to print to terminal:

class point(object):

def __init__(self, x, y):

    self.x = float(x);
    self.y = float(y);

def __str__(self):

    return("(" + self.x + "," + self.y + ")")


def main():

    first = point(2,3)
    print(first)


if __name__ == '__main__':
    main()
Sign up to request clarification or add additional context in comments.

2 Comments

why don't I have to print "first.__str__"? isn't that the only way for "str" to be used?
first.__str__ just points to the function you have defined for the object "point". This function, when called, will return a string describing you object. If you use the "print()" function, python will automatically try to make a string representation of your object to print, otherwise the output would not be human readable in most cases. The print-function will therefore call the __str__() function itself behind the scenes. You can also use print(first.__str__()) yourself, but you dont have to (and don't forget the parentheses).
0

As others have mentioned, you need to add a print() call in your main function in order to get output to the terminal. Additionally, a few other things to note: Python doesn't require semi-colons, so there's no need for them in your __init__() method and you can't implicitly convert floats to strings, so you have to be explicit in your __str__() method.

class point(object):
    def __init__(self, x, y):
        self.x = float(x)
        self.y = float(y)

    def __str__(self):
        # explicitly cast floats to strings
        return "(" + str(self.x) + "," + str(self.y) + ")"

def main():
    first = point(2,3)
    print(first)

if __name__ == '__main__':
    main()

Also, as MrTrustworthy pointed out, you don't need to call Class.__str__() directly as print first calls the __str__() method associated with the class. If there's no such method and implicit conversion fails, then you will get the representation of the calling function and the address in memory.

If you remove __str__() from your example, you should get output like this: <__main__.point object at 0x7f184cec4b70> if you try to print the object.

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.