Estimated reading time: 2 minutes
I was recently working on our last blog post how to reverse a string in python and I came across this error.
The thought passed me what does it mean and how can I fix it?
So what does the error actually mean?
Essentially it means that , you are trying to access a type of an object, that has a property of “type”.
What is property of type? Well it is :
The above alllow you to change your data to these data types, so the data contained within them can be further manipulated.
In essence you are trying to use a type in the wrong way and in the wrong place in your code.
By calling it , it will throw this error, and they should be avoided, as a it is a built in function.
Lets take an example of how we can replicate this error and fix it
name1 = "joe" emptylist = strlength = len(name1) while strlength > 0: emptylist += str[ strlength -1 ] strlength = strlength - 1 print(emptylist)
In the above code all appears well, but in line 5 the “str” before the [ is the problem. The code automatically looks to call this function.
The simple answer to fixing this is to rename it to name1 as follows:
name1 = "joe" emptylist = strlength = len(name1) while strlength > 0: emptylist += name1[ strlength -1 ] print(type(emptylist)) strlength = strlength - 1 print(emptylist)
which gives you the following error free output:
Result with no error: ['e', 'o', 'j']
In summary and what not to do
So it is clear that referencing types as a string variable should be avoided, and keep your code clean from this perspective.
This would also apply to any reserved words in Python as well.