Using closures and partial functions to eliminate redundant code
Bjoern Schliessmann
usenet-mail-0306.20.chr0n0ss at spamgourmet.com
Thu Sep 27 05:14:14 EDT 2007
Matthew Wilson wrote:
> I'm interested to hear how other people deal with really similar
> code.
> The similarity just bugs me. However, I wonder if using stuff
> like closures or partial function application is needlessly showy.
ACK -- but not because it's showy, but because it may be more
error-prone and less readable. I'd often use an approach like this:
def create_user(username, userpassword, useremail, create = False):
"""
Send an email that will update a user in the remote system.
If create evaluates to True, don't update the user, but create
her instead.
"""
if not create:
subject = "UPDATE"
else:
subject = "CREATE"
# Build email
email_body = """
USERNAME = %s
USERPASSWORD = %s
USEREMAIL = %s
""" % (username, userpassword, useremail)
# send it.
send_email(subject=subject, body=email_body)
Regards,
Björn
--
BOFH excuse #353:
Second-system effect.
More information about the Python-list
mailing list