Named code blockes

Stephen Hansen news at myNOSPAM.org
Mon Apr 23 15:16:30 EDT 2001


<James_Althoff at i2.com> wrote in message
> Now suppose the call to collection.do(xxx) is in the middle of a
> very long class def.  Then I have to put the doThisAndDoThatToItem
> def before the class or after the class thereby separating the definition
> from its one and only use by possibly hundreds of lines of
> code.  This is not nice for readability.

    With the advent of nested_scopes, I believe you can accomplish this by
just embedding a 'def' in either the __init__ or the collection.do function,
making your code as readable as that smalltalk code was. :)

For instance:

class Dummy:
    def __init__(self, collection):
        def DoThisAndDoThatToItem(item):
            item.DoThis()
            item.DoThat()

        collection.do(DoThisAndDoThatToItem)

Now. Is that readable? Its 'eh', but its far more readable then the
Smalltalkism of 'named code blocks', and it doesn't seperate that function
def from its one and only use, which is your other problem.

--S
(replace 'NOSPAM' with 'seraph' to respond in email)





More information about the Python-list mailing list