testingdebuggingprofilingand tuning use _slots_ if your program creates large number of instances of user-defined classesyou might consider using the _slots_ attribute in class definition for exampleclass stock(object) _slots_ ['name','shares','price'def __init__(self,name,shares,price)self name name self shares shares self price price _slots_ is sometimes viewed as safety feature because it restricts the set of attribute names howeverit is really more of performance optimization classes that use _slots_ don' use dictionary for storing instance data (insteada more efficient internal data structure is usedsonot only will instances use far less memorybut access to instance data is also more efficient in some casessimply adding _slots_ will make program run noticeably faster without making any other changes there is one caution with using _slots_ _however adding this feature to class may cause other code to break mysteriously for exampleit is generally well-known that instances store their data in dictionary that can be accessed as the _dict_ attribute when slots are definedthis attribute doesn' exist so any code that relies on _dict_ will fail avoid the operator whenever you use the to look up an attribute on an objectit always involves name lookup for examplewhen you say namethere is lookup for the variable name "xin the environment and then lookup for the attribute "nameon for user-defined objectsattribute lookup may involve looking in the instance dictionarythe class dictionaryand the dictionaries of base-classes for calculations involving heavy use of methods or module lookupsit is almost always better to eliminate the attribute lookup by putting the operation you want to perform into local variable first for exampleif you were performing lot of square root operationsit is faster to use 'from math import sqrtand 'sqrt( )rather than typing 'math sqrt( )in the first part of this sectionwe saw that this approach resulted in speedup of about obviously you should not try to eliminate attribute lookups everywhere in your program because it will make your code very difficult to read howeverfor performance-critical sectionsthis is useful technique use exceptions to handle uncommon cases to avoid errorsyou might be inclined to add extra checks to program for exampledef parse_header(line)fields line split(":"if len(fields! raise runtimeerror("malformed header"headervalue fields return header lower()value strip( lib fl ff