Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Data Mining Concepts And Techniques

Authors: Jiawei Han, Micheline Kamber, Jian Pei

3rd Edition

0123814790, 9780123814791

More Books

Students also viewed these Databases questions