Named code blockes

James_Althoff at i2.com James_Althoff at i2.com
Mon Apr 23 16:48:07 EDT 2001


I agree with you that embedded functions -- supported by the new
nested_scopes feature -- helps
a lot.

 I still think the nameless block approach is more readable than the named
function approach --
in this case mainly because nested functions make the code a bit more
confusing at first glance.
(I've seen inexperienced programmers have their minds boggled by the notion
of defining a function
inside of a function.)  But, this could just be a matter of preference on
my part.  Besides, I like the
Smalltalk syntax (being highly prejudiced in this regard due to having been
part of the gang that
devised it in the first place -- having said that, however, I do like the
Python indentation-based
syntax even better).

Jim



<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)


--
http://mail.python.org/mailman/listinfo/python-list






More information about the Python-list mailing list