How to override the str(s) (s.__str__()) method in a class with Python

1 Answer

0 votes
class Test(object):

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

    def __str__(self):
        return "x = {}  y = {}".format(self.x, self.y)


o = Test(13, 200)

print(o.__str__)

print(o)


'''
run:

<bound method Test.__str__ of <__main__.Test object at 0x00445A70>>
x = 13  y = 200

'''

 



answered Sep 30, 2017 by avibootz

Related questions

1 answer 154 views
154 views asked Dec 11, 2020 by avibootz
1 answer 114 views
114 views asked Oct 16, 2022 by avibootz
1 answer 168 views
168 views asked Nov 9, 2021 by avibootz
1 answer 256 views
1 answer 185 views
...