[Python-Dev] anonymous blocks

Guido van Rossum gvanrossum at gmail.com
Wed Apr 20 21:55:47 CEST 2005

[Shane Holloway]
> When I
> write classes, I tend to put the public methods at the top.  Utility
> methods used by those entry points are placed toward the bottom.  In
> this way, I read the context of what I'm doing first, and then the
> details of the internal methods as I need to understand them.
> Granted I could achieve this effect with::
>      class Before:
>          def readIt(self, filename):
>              def readIt():
>                  withFile(filename, doReading)
>              def doReading(aFile):
>                  self.readPartA(aFile)
>                  self.readPartB(aFile)
>                  self.readPartC(aFile)
>              return readIt()
> Which is fine with me, but the *intent* is more obfuscated than what the
> block construct offers.  And I don't think my crew would appreciate if I
> did this very often.  ;)

I typically solve that by making doReading() a method:

class Before:

    def readit(self, filename):
        withFile(filename, self._doReading)

    def _doReading(self, aFile):

Perhaps not as Pure, but certainly Practical. :-) And you could even
use __doReading to make it absolutely clear that doReading is a local
artefact, if you care about such things.

--Guido van Rossum (home page: http://www.python.org/~guido/)

More information about the Python-Dev mailing list