Skip to content Skip to sidebar Skip to footer

Typeerror: Int() Argument Must Be A String Or A Number, Not 'binary'

I'm working through http://blog.thedigitalcatonline.com/blog/2015/05/13/python-oop-tdd-example-part1/#.VxEEfjE2sdQ . I'm working through this iteratively. At this point I have the

Solution 1:

The int function cannot deal with user defined classes unless you specify in the class how it should work. The __int__ (not init) function gives the built-in python int() function information regarding how your user defined class (in this case, Binary) should be converted to an int.

classBinary:
    ...your init here
    def__int__(self):
        returnint(self.value) #assuming self.value is of type int

Then you should be able to do things like.

printint(Binary(0x3)) #should print 3

I might also suggest standardizing the input for the __init__ function and the value of self.value. Currently, it can accept either a string (e.g '0b011' or a 0x3) or an int. Why not just always make it accept a string as input and always keep self.value as an int.

Post a Comment for "Typeerror: Int() Argument Must Be A String Or A Number, Not 'binary'"