diff --git a/iterators.html b/iterators.html index 398e3ce..169138d 100755 --- a/iterators.html +++ b/iterators.html @@ -371,6 +371,11 @@ rules = LazyRules()
++☞Is this really nirvana? Well, yes and no. Here’s something to consider with the
LazyRulesexampl: the pattern file is opened (during__init__(), and it remains open until the final rule is reached. Python will evenutally close the file when it exits, or after the last instantiation of theLazyRulesclass is destroyed, but still, that could be a long time. If this class is part of a long-running Python process, the Python interpreter may never exit, and theLazyRulesobject may never get destroyed. +There are ways around this. Instead of opening the file during
__init__()and leaving it open while you read rules one line at a time, you could open the file, read all the rules, and immediately close the file. Or you could open the file, read one rule, save the file position with thetell()method, close the file, and later re-open it and use theseek()method to continue reading where you left off. Or you could not worry about it and just leave the file open, like this example code does. Programming is design, and design is all about trade-offs and constraints. Leaving a file open too long might be a problem; making your code more complicated might be a problem. Which one is the bigger problem depends on your development team, your application, and your runtime environment. +
⁂