Question
Python 3.7.3 Implement a class MyInt() that behaves almost the same as the class int, except when trying to add an object of type MyInt.
Python 3.7.3
Implement a class MyInt() that behaves almost the same as the class int, except when trying to add an object of type MyInt. Then, this strange behavior occurs:
>>> x = MyInt(5)
>>> x*4
20
>>> x*(4 + 6)
50
>>> x + 6
'Whatever ...'
>>>
I have written this code
class myInt : def __init__(self, val=0) : self._val = int(val) def __add__(self, val) : return "Whatever ..." def __iadd__(self, val) : self._val += val return self def __str__(self) : return str(self._val)
def __mul__(self, other): return myInt(self._val * other)
but I run into this
>>> x = myInt(5) >>> x*4 <__main__.aInt object at 0x0336EFD0>
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