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:

list_of_records = [Person(*line.split()) for line in f]

An empty line results in an empty list:

>>> n.split()
[]

which leads to Person(*[]) being called, so a Person() instance was created with no arguments, leaving the default firstname=None.

Skip empty lines:

list_of_records = [Person(*line.split()) for line in f if line.strip()]

You may also want to default to empty strings, or specifically test for None values before treating attributes as strings:

def searchFName(self, matchString):
    return bool(self.fname) and matchString.lower() in self.fname.lower()

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:

>>> p = Person()
>>> p.fname is None
True
>>> p.searchFName(foo)
False

AttributeError: NoneType object has no attribute lower python

Leave a Reply

Your email address will not be published. Required fields are marked *