What is an "AttributeError"?
An attribute error is when an attribute doesn't exist in an object.
class object: attrOne = "Hello " attrTwo = "World" print( object.attrOne + object.attrTwo + object.thisAttrDoesntExist)
Traceback (most recent call last):
File "", line 5, in
AttributeError: type object 'object' has no attribute 'thisAttrDoesntExist'
This is because in the example, we never declared
thisAttrDoesntExist. We only declared
thisAttrDoesntExist from the class
object would return an
AttributeError as the attribute doesn't exist.
To solve this problem, we need to change the example a bit:
class object: attrOne = "Hello " attrTwo = "World" thisAttrDoesntExist = " It exists!" print( object.attrOne + object.attrTwo + object.thisAttrDoesntExist)
Hello World It exists!
It works because the
object now has the attribute declared. It now exists. So now the name is a bit deceiving isn't it?