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

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!