AttributeError: NoneType object has no attribute lower python

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  :

Leave a Reply

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