Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Object-Oriented Python Advanced Objects Controlling Conversion

Elena Chen
Elena Chen
1,503 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)
>>>str(five)

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

Where am I doing wrongly?

1 Answer

Steven Parker
Steven Parker
229,644 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"