Greg Perry <gregp <at> liveammo.com> writes: > Is it safe to say that classes are only useful for instances where reuse is a key consideration? From my very > limited perspective, it seems that classes are in most cases overkill for simple tasks (such as reading > the command line then calculating a hash/checksum to verify integrity).
Alan has already adressed your questions, I just have one addition on this point. If this is *all* your application does, a class is indeed overkill. However, if this functionality is part of a larger application, it could become a small class. Imagine an application that would offer some file operations, like: - calculate hash - split into chunks of 1.44 MB - compress - encrypt You could have a generic ancestor class, say FileOperation and inherit from it different classes implementing specific operations. class FileOperation(object): def __init__(self, filename): self._filename = filename def Perform(self): pass # should be implemented in children def ShowHelp(self): pass # should be implemented in children class HashFileOperation(FileOperation): def Perform(self): # calculate hash of self._filename def ShowHelp(self): print "Calculates MD5 hash of the file" <etc> Depending on a command line option, the application could instantiate one of the operation classes and work with it. Assuming the command line would be 'app.py <operation> <filename>' (e.g. 'app.py hash myfile.txt'), the application code could look something like this: # register all known operation classes and bind them # to specific command line options operations = {'hash': HashFileOperation, 'split': SplitFileOperation, <etc>} opname = sys.argv[1] # determine what operation class is necessary opclass = operations[opname] # instantiate that operation (make a file operation object) fileop = opclass(sys.argv[2]) # check if it should display help or run the operation if sys.argv[2] == '?': fileop.ShowHelp() else: fileop.Perform() Adding new operations would be a matter of implementing an appropriate class and adding it to the operations dictionary. With a bit of Python magic you could even get the operation classes to auto-register, so just writing an operation class would automatically make it available in the application. -- Yours, Andrei ===== Mail address in header catches spam. Real contact info: ''.join([''.join(s) for s in zip( "poet <at> aao.l pmfe!Pes ontuei ulcpss edtels,s hr' one oC.", "rjc5wndon.Sa-re laed o s npbi ot.Ira h it oteesn edt C")]) _______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor