Unexpected Attribute Error when running the given python code