[Edu-sig] erratum? Bioinformatics book...

kirby urner kirby.urner at gmail.com
Thu Mar 31 21:43:59 EDT 2016


I wonder if anyone else out there in subscriber-world has Python
Programming for Biology, by Stevens and Boucher.

I'm enjoying it and learning a lot.  PDB files...

On page 75 this is given as the wrong way to pass a function for which you
wish to set up some default parameters:

def jobFunc(arg1, errorFunc('Warning', color='Red')) # Wrong

I agree that's wrong because we don't want to be calling the function it
this time merely passing it in as an object still uncalled, arguments
pre-set.

This errorFunc collapses to an object, gets evaluated prematurely.

But then the "right way" (same page) is given as:

def jobFunc(arg1, lambda: errorFunc('Warning', color="red')):

I'd say that's wrong too as we've not bound this function to a name, so how
in the function body is errorFunc to be triggered?  No object is queued.

I'd go with:

def jobFunc(arg1, to_do = lambda x='Warning', color='red': errorFunc(x,
color)):

which gives the caller a way to override the defaults and not use errorFunc
at all maybe.

Otherwise, if the defaults are acceptable, just call to_do() in the body,
with no args, and all will be well.

Another pattern:

def jobFunc(arg1, message = 'Warning', color='red'):
    # ...
    try:
        # etc. -- whatever it is that might not work
        pass
    except:
        errorFunc(message, color)

That's assuming errorFunc is always appropriate and we're only concerned to
maybe override its default arguments.

That could work too.

Kirby
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/edu-sig/attachments/20160331/7a849b27/attachment.html>


More information about the Edu-sig mailing list