is there any principle when writing python function

rantingrick rantingrick at gmail.com
Fri Aug 26 20:05:30 CEST 2011


On Aug 26, 10:40 am, John Gordon <gor... at panix.com> wrote:
> In <7b47ca17-d3f1-4d91-91d1-98421e870... at ea4g2000vbb.googlegroups.com> rantingrick <rantingr... at gmail.com> writes:
>
> > Furthermore: If you are moving code out of one function to ONLY be
> > called by that ONE function then you are a bad programmer and should
> > have your editor taken away for six months. You should ONLY create
> > more func/methods if those func/methods will be called from two or
> > more places in the code. The very essence of func/meths is the fact
> > that they are reusable.
>
> That's one very important aspect of functions, yes.  But there's another:
> abstraction.
> [...]
> The main module keeps a high level of abstraction instead of descending
> into dozens or even hundreds of lines of LDAP-specific code.

Exactly. I am not arguing against creating intuitive and
simplistically elegant interfaces. I mean, lists *could* have only one
method called apply(process, *args, **kw) which takes an argument like
("append", "value") or ("index", 42) and has a long block of logic to
handle the inputs however that would be a horrible interface.

So in that respect i agree. We must weigh the entire interface from an
empirical perspective. However i can be sure of one point: As you
increase the number of methods you also increase the mental load
required to understand that particular interface.

An interface with a small number of methods will not suffer too
terribly from one or two extra methods however at some point more
methods just equals more confusion. It is a delicate balancing act
that many programmers are not agile enough to juggle elegantly.

Take for instance the interface for Grep, Search, and Replace dialogs
in the idlelib which span two separate modules and have a mind numbing
number of methods for such remedial things as creating buttons and
entrys. All three dialogs look very similar and share many
similarities.

Now take a look at MY simple ONE module solution. It has JUST enough
methods and NOT a single more! Yes the create widgets method is fairly
long (weighing in at 80+ lines with comments!) however all of this
code needs to be contained in ONE and ONLY one method. Heck if i
wanted to get pendantic i could replace the five cb_*() methods with
partials however MY interface is so intuitive there is no need.

############################################################
# START CODE
############################################################
class FindReplaceDialog(object):
    def __init__(self, textbox):
        [...]

    def create_widgets(self, type_):
        # Create toplevel dialog window.
        [...]
        # Create widgets belonging to both
        # search AND replace dialogs dialogs.
        [...]
        if type_ == 'replace':
            # Add widgets unique to replace
            # dialogs.
            [...]
        elif type_ == 'grep':
            # Add widgets unique to grep
            # dialogs.
            [...]
        # Load any initial values and states.
        [...]

    def show(self, type_='find'):
        self.create_widgets(type_)
        # Do any initial setup.

    def close(self, event=None):
        # destroy the dialog.

    def find_again(self, event=None):
        # Event callback bound to textbox.

    def find(self, target):
        # Search the buffer for target and
        # hilight if found.

    def replace(self, action='replace'):
        # Fetch the old and new strings and
        # mediate the work depending on the
        # action.
        [...]
        if action == 'replace+find':
            [...]
        elif action == 'replaceall':
            [...]

    def grep():
        [...]

    def cb_grepbutton(self, event=None):
        self.grep(target.entry.get())

    def cb_findbutton(self, event=None):
        self.find(target.entry.get())

    def cb_replacebutton(self):
        self.replace(action='replace')

    def cb_replacefindbutton(self):
        self.replace(action='replace+find')

    def cb_replaceallbutton(self):
        self.replace(action='replaceall')
############################################################
# END CODE
############################################################

Now look at the three modules in idlelib (Grep Dialog, Search Dialog,
and Replace Dialog) and ask yourself which is cleaner? Which is more
intuiitve? Which is more professional? Which would you rather debug?

*scholl-bell-rings*



More information about the Python-list mailing list