[Python-Dev] anonymous blocks

Aahz aahz at pythoncraft.com
Wed Apr 20 17:15:07 CEST 2005


On Tue, Apr 19, 2005, Shane Holloway (IEEE) wrote:
>
> I heartily agree!  Especially when you have very similar try/finally 
> code you use in many places, and wish to refactor it into a common area. 
>  If this is done, you are forced into a callback form like follows::
> 
> 
>     def withFile(filename, callback):
>         aFile = open(filename, 'r')
>         try:
>            result = callback(aFile)
>         finally:
>            aFile.close()
>         return result
> 
>     class Before:
>         def readIt(self, filename):
>             def doReading(aFile):
>                 self.readPartA(aFile)
>                 self.readPartB(aFile)
>                 self.readPartC(aFile)
> 
>             withFile(filename, doReading)
> 
> Which is certainly functional.  I actually use the idiom frequently. 
> However, my opinion is that it does not read smoothly.  This form 
> requires that I say what I'm doing with something before I know the 
> context of what that something is.  For me, blocks are not about 
> shortening the code, but rather clarifying *intent*.  

Hmmmm....  How is this different from defining functions before they're
called?
-- 
Aahz (aahz at pythoncraft.com)           <*>         http://www.pythoncraft.com/

"The joy of coding Python should be in seeing short, concise, readable
classes that express a lot of action in a small amount of clear code -- 
not in reams of trivial code that bores the reader to death."  --GvR


More information about the Python-Dev mailing list