Answered step by step
Verified Expert Solution
Question
1 Approved Answer
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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started