Python Object-Oriented Python Advanced Objects Controlling Conversion

Elena Chen
Elena Chen
1,500 Points

Why am I getting this error? AttributeError: type object 'str' has no attribute 'value'

class NumString:
    def __init__(self, value):
        self.value = str(value)

    def __str__(self):
        return self.value

    def __int__(self):
        return int(self.value)

    def __float__(self):
        return float(self.value)

in the Shell, I did as Kenneth did:

>>>from numstring import NumString
>>>five = NumString(5)

the error message shows: AttributeError: type object 'str' has no attribute 'value'

Where am I doing wrongly?

1 Answer

Steven Parker
Steven Parker
194,950 Points

This code seems fine, are you sure this is the code you are using?

I can cause the same error if I make one change in the code:

    def __str__(self):
        return str.value  # changed "self" to "str"