Newbie Pythonista here.
I am having a bit of an issue. I want to print a specific output which needs to be like Point(x=1, y=2, z=3) (xyz can of course be different in values).
This is the code:
class Point:
def __init__(self, x, y, z):
self.x = x
self.y = y
self.z = z
def __str__(self):
return f"Point(x={self.x}, y={self.y}, z={self.z})"
This is the test code:
import unittest
from point import Point
class PointTests(unittest.TestCase):
"""Tests for Point."""
def test_attributes(self):
point = Point(1, 2, 3)
self.assertEqual((point.x, point.y, point.z), (1, 2, 3))
point.x = 4
self.assertEqual(point.x, 4)
def test_string_representation(self):
point = Point(1, 2, 3)
self.assertEqual(str(point), 'Point(x=1, y=2, z=3)')
self.assertEqual(repr(point), 'Point(x=1, y=2, z=3)')
point.y = 4
self.assertEqual(str(point), 'Point(x=1, y=4, z=3)')
self.assertEqual(repr(point), 'Point(x=1, y=4, z=3)')
def test_equality_and_inequality(self):
p1 = Point(1, 2, 3)
p2 = Point(1, 2, 4)
p3 = Point(1, 2, 3)
self.assertNotEqual(Point(1, 2, 3), Point(1, 2, 4))
self.assertEqual(Point(1, 2, 3), Point(1, 2, 3))
self.assertFalse(Point(1, 2, 3) != Point(1, 2, 3))
self.assertNotEqual(p1, p2)
self.assertEqual(p1, p3)
p3.x, p3.z = p3.z, p3.x
self.assertNotEqual(p1, p3)
self.assertTrue(p1 != p3)
self.assertFalse(p1 == p3)
The problem I am having is that in str function when I am using a "Point" in the print format I am thrown with an assertion error:
AssertionError: 'point.Point object at 0x7fbd8850b190' != 'Point(x=1, y=2, z=3)'
But when I use anything else it gets printed. For example lets say I use points instead of Point:
'points(x=1, y=2, z=3)' != 'Point(x=1, y=2, z=3)'
Why does that happen and how can I get around this?