Question: Quick help with Python code ( Python only) working on __Str__ and __repr__ methods for this code class Assign(Expr): Assignment: x = E represented as

Quick help with Python code ( Python only)

working on __Str__ and __repr__ methods for this code

class Assign(Expr): """Assignment: x = E represented as Assign(x, E)""" def __init__(self, left: Var, right: Expr): assert isinstance(left, Var) # Can only assign to variables! self.left = left self.right = right def eval(self) -> IntConst: r_val = self.right.eval() self.left.assign(r_val) return r_val def __str__(self): return f"({self.left} = {self.right})" def __repr__(self): return f"({repr(self.left)} = Var{repr(self.right)})"

This is the test case is should be able to pass

def test_assign_reps(self): v = Var("v") w = Var("w") exp = Assign(v, Plus(IntConst(5), w)) self.assertEqual(str(exp), "(v = (5 + w))") self.assertEqual(repr(exp), "Assign(Var(v), Plus(IntConst(5), Var(w)))") 

But it keeps failing cause the format doesnt match please help

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!