Python class exercise questions

Asked 2 years ago, Updated 2 years ago, 68 views

I have to make a class named MyInteger

(1)variable_01 = MyInteger(1)

(2)(2)variable_02 = MyInteger(2.0)

(3)(3)variable_03 = MyInteger('3')

(4)(4)variable_04 = MyInteger([4,5,6])

(5)(5)variable_05 = variable_01 + '10'

variable_01==1

variable_02==2

variable_03==3

variable_04==0

variable_05==11

How do I code to get this value?

    class MyInteger():
    def __init__(self,variable):
        self.Num = 0
        if type(variable) == int :
            self.Num= 1
        elif type(variable) == float:
            self.Num = 2
        elif type(variable) == str:
            self.Num = 3
        elif type(variable) == list:
            self.Num = 0

    def __str__(self): 
        return str(self.Num)

So we came up to variable_04.

The variable_05 value cannot be derived as 11.

python class

2022-09-20 13:12

1 Answers

Search for operator overloading


2022-09-20 13:12

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.