AttributeError: NoneType object has no attribute lower python
AttributeError: NoneType object has no attribute lower python
You have a Person()
class with no firstname
. You created it from an empty line in your file:
n
list_of_records = [Person(*line.split()) for line in f]n
n
An empty line results in an empty list:
n
>>> \n.split()n[]n
n
which leads to Person(*[])
being called, so a Person()
instance was created with no arguments, leaving the default firstname=None
.
n
Skip empty lines:
n
list_of_records = [Person(*line.split()) for line in f if line.strip()]n
n
You may also want to default to empty strings, or specifically test for None
values before treating attributes as strings:
n
def searchFName(self, matchString):n return bool(self.fname) and matchString.lower() in self.fname.lower()n
n
Here bool(self.fname)
returns False
for empty or None
values, giving you a quick False
return value when there is no first name to match against:
n
>>> p = Person()n>>> p.fname is NonenTruen>>> p.searchFName(foo)nFalsen
‘
AttributeError: NoneType object has no attribute lower python
Related posts on Attribute Error :
- AttributeError: NoneType object has no attribute lower python
- numpy – Python AttributeError: function object has no attribute min
- parallel processing – Python multiprocessing.Pool: AttributeError
- python – AttributeError: dict object has no attribute iteritems
- python – Lists: AttributeError: str object has no attribute append
- python – AttributeError: DataFrame object has no attribute map
- python – AttributeError: NoneType object has no attribute format
- python dictionary error AttributeError: list object has no attribute keys
- Python regex AttributeError: NoneType object has no attribute group