A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://mail.python.org/pipermail/python-dev/2005-April/052764.html below:

[Python-Dev] anonymous blocks

[Python-Dev] anonymous blocksAahz 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

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4