diff --git a/special-method-names.html b/special-method-names.html index 3da0b56..da77766 100644 --- a/special-method-names.html +++ b/special-method-names.html @@ -289,12 +289,13 @@ class FieldStorage: return any(item.name == key for item in self.list) ③ def __len__(self): ④ - return len(self.keys()) + return len(self.keys()) ⑤
cgi.FieldStorage class, you can use the “in” operator to check whether a particular parameter was included in the query string.
__contains__() method is the magic that makes this work.
if 'q' in fs, Python looks for the __contains__() method on the fs object, which is defined in cgi.py. The value 'q' is passed into the __contains__() method as the key argument.
FieldStorage class also supports returning its length, so you can say len(fs) and it will call the __len__() method on the FieldStorage class to return the number of query parameters that it identified.
+self.keys() method checks whether self.list is None, so the __len__ method doesn’t need to duplicate this error checking.