russell.ballestrini.net/content/2013-01-12-how-to-overload-default-function-arguments-in-python-using-lambda.rst
russellballestrini 2319e768a6 new file: content/2011-01-02-a-homegrown-python-bread-crumb-module.rst
new file:   content/2011-01-05-how-did-stack-overflow-get-initial-traction.rst
	new file:   content/2011-01-09-graphic-design-powerhouse-wacom-bamboo-ubuntu-and-mypaint.rst
	new file:   content/2011-01-10-i-just-used-google-to-purchase-chinese-food-takeout-for-two.rst
	new file:   content/2011-01-11-response-to-l-theanine-a-4000-year-old-mind-hack.rst
	new file:   content/2011-03-20-virt-back-a-python-libvirt-backup-utility-for-kvm-xen-virtualbox.rst
	new file:   content/2011-03-26-porting-the-chaostheory-wordpress-theme-to-pylowiki.rst
	new file:   content/2011-04-03-voice-over-ip-with-teamspeak.rst
	new file:   content/2011-04-16-dropbox-encryption-with-truecrypt.rst
	new file:   content/2011-06-11-connecticut-killed-affliate-marketing-with-amazon-com.rst
	new file:   content/2011-06-18-att-uverse-residential-gateway-broadband-led-flashes-red-intermittently.rst
	new file:   content/2011-06-25-google-bot-attempts-to-crawl-shortest-urls-first.rst
	new file:   content/2011-07-02-add-a-breadcrumb-subscriber-to-a-pyramid-project-using-4-simple-steps.rst
	new file:   content/2011-07-11-a-hack-to-gain-80-percent-efficiency-when-creating-github-projects.rst
	new file:   content/2011-08-07-the-great-gist-heist.rst
	new file:   content/2011-08-08-programming-is-like-alchemy.rst
	new file:   content/2011-08-10-virt-back-restoring-from-backups.rst
	new file:   content/2011-08-18-security-professionals-yes-we-appear-vulnerable-but-that-attack-vector-will-never-happen.rst
	new file:   content/2011-08-22-python-image-grabber-pig-py.rst
	new file:   content/2011-09-06-deliberating-the-viewers-vs-doers-concept.rst
	new file:   content/2011-09-08-a-system-administrators-guide-to-installing-and-maintaining-multiple-python-environments.rst
	new file:   content/2011-09-09-hacker-olive-oil-lamp-crafted-from-home-materials.rst
	new file:   content/2011-09-27-new-baby-homecoming.rst
	new file:   content/2011-10-11-css-frameworks-not-rendering-properly-on-all-browsers.rst
	new file:   content/2011-10-12-a-better-way-to-show-website-backlinks.rst
	new file:   content/2011-10-15-i-cancelled-my-xbox-live-automatic-renewal.rst
	new file:   content/2011-10-15-r8168-driver-issues-after-ubuntu-11-10-upgrade-kernel-linux-3-0.rst
	new file:   content/2011-10-16-cell-shading-practice-a-sketched-skull-and-wind-mill-photograph.rst
	new file:   content/2011-10-20-adding-inline-image-support-to-a-gmail-messages.rst
	new file:   content/2011-10-21-occupy-wall-street-stack-vs-queue.rst
	new file:   content/2011-10-29-webmaster-tools-alerted-issue-turned-out-pylon-session-files-flooded-inodes.rst
	new file:   content/2011-11-03-im-petrified-of-launching-my-web-application.rst
	new file:   content/2011-11-29-career-development-is-a-game-of-chutes-and-ladders.rst
	new file:   content/2011-11-30-how-to-incorporate-custom-configuration-in-a-pyramid-application.rst
	new file:   content/2011-12-05-linkpeek-com-number-one-on-hacker-news.rst
	new file:   content/2011-12-12-flash-mob-office-meeting-definition.rst
	new file:   content/2012-01-18-today-i-lost-a-customer.rst
	new file:   content/2012-02-08-reasons-why-some-internet-entities-might-want-secure-communication.rst
	new file:   content/2012-02-08-what-are-the-differences-between-message-confidentiality-and-message-integrity.rst
	new file:   content/2012-02-13-attributes-of-an-8-block-cipher.rst
	new file:   content/2012-02-13-monoalphabetic-cipher-and-inverse-written-in-python.rst
	new file:   content/2012-02-13-symmetric-encryption-vs-public-key-encryption.rst
	new file:   content/2012-02-13-why-does-a-hash-provide-better-message-integrity-then-an-internet-checksum.rst
	new file:   content/2012-02-14-block-cipher-lab.rst
	new file:   content/2012-02-16-my-4-month-olds-15-minutes-of-fame.rst
	new file:   content/2012-02-18-jake.rst
	new file:   content/2012-02-26-zenoss-or-nagios-monitoring-of-https-using-client-certificate-authentication.rst
	new file:   content/2012-02-29-how-to-capture-https-ssl-tls-packets-with-wireshark.rst
	new file:   content/2012-03-03-how-to-save-hundreds-of-dollars-on-groceries-without-clipping-coupons.rst
	new file:   content/2012-03-07-what-do-you-name-your-python-virtualenv.rst
	new file:   content/2012-03-23-i-see-said-the-blind-man-to-the-deaf-dog-as-he-walked-off-the-cliff.rst
	new file:   content/2012-03-26-nosslsearch-cname-is-a-bad-idea-and-solution.rst
	new file:   content/2012-03-27-trouble-mounting-filesystem-on-kvm-guest-after-reboot.rst
	new file:   content/2012-04-09-i-just-purchased-instagram-for-1b-and-all-i-got-was-this-lousy-image-filter.rst
	new file:   content/2012-04-26-the-most-valuable-registration-field-how-did-you-hear-about-us.rst
	new file:   content/2012-05-11-how-to-rescue-logs-and-config-from-a-failed-citrix-netscaler-app-gateway.rst
	new file:   content/2012-05-19-high-load-on-web-server-after-updating-from-ubuntu-10-04-to-ubuntu-12-04-lts.rst
	new file:   content/2012-06-10-always-attempt-to-scale-vertically-first.rst
	new file:   content/2012-06-13-prevent-a-certain-program-from-running-to-long-in-bash.rst
	new file:   content/2012-06-25-my-top-five-suggestions-for-an-independent-developer-creating-a-new-product-or-service.rst
	new file:   content/2012-06-29-miniuri-parser-and-ago-human-timedelta.rst
	new file:   content/2012-09-22-the-pyramid-community-taught-me-the-importance-of-test-driven-development.rst
	new file:   content/2012-10-02-explaining-cache-with-python.rst
	new file:   content/2012-11-03-how-to-drain-an-iphone-battery-without-needing-passcode.rst
	new file:   content/2012-12-12-tips-for-getting-pull-requests-approved.rst
	new file:   content/2013-01-09-ago-py-human-readable-timedelta0-0-4-release.rst
	new file:   content/2013-01-12-how-to-overload-default-function-arguments-in-python-using-lambda.rst
	new file:   content/2013-03-02-virt-backs-domfetcher-class-returns-doms-from-libvirt-api.rst
	new file:   content/2013-03-22-guido-name-dropped-tornado-python-tulip-and-pep-3156.rst
	new file:   content/2013-04-09-automatic-event-hangout-with-cron.rst
	new file:   content/2013-04-26-survey-baby-monkey.rst
	new file:   content/2013-05-26-honey-i-just-deleted-linkpeek-com.rst
	new file:   content/2013-06-20-high-load-and-cpu-usage-craftbukkit-compared-to-vanilla-minecraft.rst
	new file:   content/2013-07-01-add-a-custom-header-to-all-salt-managed-files-using-pillar-and-jinja-templates.rst
	new file:   content/2013-07-02-create-your-own-fleet-of-servers-with-digital-ocean-and-salt-cloud.rst
	new file:   content/2013-07-08-understanding-salt-stack-user-and-group-management.rst
	new file:   content/2013-07-11-configuration-management-vs-remote-execution.rst
	new file:   content/2013-10-14-control-a-mongodb-collection-in-configuration-management.rst
	new file:   content/2013-10-17-postfix-salt-state-formula.rst
	new file:   content/2013-10-19-simplify-deployments-with-upstart-and-uwsgi.rst
	new file:   content/2013-10-27-backup-all-virtual-machines-on-a-smartos-hypervisor-with-smart-back-sh.rst
	new file:   content/2013-12-13-hackathon-2013-virtualization.rst
	new file:   content/2013-12-17-sensu-salt.rst
	new file:   content/2013-12-20-heka-world.rst
	new file:   content/2014-01-02-test-game-engine-with-python-and-sfml.rst
	new file:   content/2014-01-05-how-i-added-two-seagate-240g-ssds-as-smartos-l2arc.rst
	new file:   content/2014-01-25-how-to-reset-hp-ilo-lights-out-user-and-password-settings-with-ipmitools.rst
	new file:   content/2014-02-03-the-three-deployment-management-strategies.rst
	new file:   content/2014-02-07-automatic-backups.rst
	new file:   content/2014-02-07-mysql-back.rst
	new file:   content/2014-02-07-tar-back.rst
	new file:   content/2014-02-21-configuration-management-and-the-golden-image.rst
	new file:   content/2014-03-08-replace-the-nagios-scheduler-and-nrpe-with-salt-stack.rst
	new file:   content/2014-03-13-filter-salt-stack-return-data-output.rst
	new file:   content/2014-03-15-irc-bot-foxbot-runs-canned-remote-executions-using-salt-stack.rst
	new file:   content/2014-04-08-how-to-patch-heartbleed-openssl-defect-libssl-on-ubuntu.rst
	new file:   content/2014-05-08-nginx-with-ssl-and-mixed-content-errors-with-upstream-wsgi-servers.rst
	new file:   content/2014-05-15-you-can-hack-on-freenas-9.rst
	new file:   content/2014-08-15-mailpile-salt-states-for-ubuntu-or-debian.rst
	new file:   content/2014-08-23-heka-world2.rst
	new file:   content/2014-09-18-rabbits.rst
	new file:   content/2014-09-18-reality-shattered.rst
	new file:   content/2014-10-11-migrating-mongodb-from-ubuntu-to-smartos.rst
	new file:   content/2014-10-11-set-root-password-smartos-percona-mysql-zone.rst
	new file:   content/2014-11-09-turn-python-dict-into-a-keyvalue-string.rst
	new file:   content/2014-11-13-dealing-with-pagination-in-python.rst
	new file:   content/2014-11-24-build-release-pipelines-on-s3-with-s3p.rst
	new file:   content/2014-12-03-custom-rundeck-hipchat-notification-templates.rst
	new file:   content/2014-12-21-autofs-net-automount-stopped-working.rst
	new file:   content/2015-01-10-risk-process-and-balance.rst
	new file:   content/2015-01-21-set-postgres-user-password-on-postgresql-smartos-zone.rst
	new file:   content/2015-02-22-securely-publish-jenkins-build-artifacts-on-salt-master.rst
	new file:   content/2015-04-24-setting-region-programmatically-in-boto3.rst
	new file:   content/2015-04-28-migrating-libvirt-kvm-guest-to-smartos-kvm-guest.rst
	new file:   content/2015-05-08-a-python-script-which-searches-for-available-interpreters.rst
	new file:   content/2015-05-09-smartos-ubuntu-guest-apt-get-not-working-because-ipv6.rst
	new file:   content/2015-06-01-change-default-gateway-on-all-smartos-zones-and-kvm-guests.rst
	new file:   content/uploads/2010/11/thebarrymores.png
	new file:   content/uploads/2010/12/board.png
	new file:   content/uploads/2010/12/create.png
	new file:   content/uploads/2010/12/four2go.png
	new file:   content/uploads/2010/12/gumyumgameslogo.png
	new file:   content/uploads/2010/12/history.png
	new file:   content/uploads/2010/12/stats.png
	new file:   content/uploads/2010/12/usermenu.png
	new file:   content/uploads/2011/01/MyPaint.png
	new file:   content/uploads/2011/01/bamboo.jpg
	new file:   content/uploads/2011/01/dream.jpg
	new file:   content/uploads/2011/01/food1.jpg
	new file:   content/uploads/2011/01/pot.png
	new file:   content/uploads/2011/01/stack.png
	new file:   content/uploads/2011/01/stack1.png
	new file:   content/uploads/2011/01/treeman1.png
	new file:   content/uploads/2011/03/kvmbanner-logo2.png
	new file:   content/uploads/2011/03/virt-back.png
	new file:   content/uploads/2011/04/onion.png
	new file:   content/uploads/2011/04/truecrypt.png
	new file:   content/uploads/2011/06/access-school.yohdah.log_.zip
	new file:   content/uploads/2011/06/new-splitter.jpg
	new file:   content/uploads/2011/06/old-splitter.jpg
	new file:   content/uploads/2011/06/school.yohdah.com_.graph_.png
	new file:   content/uploads/2011/06/us-public-schools.png
	new file:   content/uploads/2011/06/us-public-schools1.png
	new file:   content/uploads/2011/06/uverse-dsl-link-errors.jpg
	new file:   content/uploads/2011/07/80-percent-github-hack.png
	new file:   content/uploads/2011/08/Alchemy_2.jpg
	new file:   content/uploads/2011/08/Golem-soul-caliber.jpg
	new file:   content/uploads/2011/08/Golem-soul-caliber1.jpg
	new file:   content/uploads/2011/08/password-strength.png
	new file:   content/uploads/2011/08/pig.png
	new file:   content/uploads/2011/08/pig1-color1.png
	new file:   content/uploads/2011/08/pig1.png
	new file:   content/uploads/2011/08/pig2.png
	new file:   content/uploads/2011/08/pig3.png
	new file:   content/uploads/2011/08/pig4.png
	new file:   content/uploads/2011/08/pig5.png
	new file:   content/uploads/2011/08/pig6.png
	new file:   content/uploads/2011/08/pig7.png
	new file:   content/uploads/2011/08/pigpy.xcf
	new file:   content/uploads/2011/08/the-great-gist-heist.torrent
	new file:   content/uploads/2011/08/time-travelling-wizard-alchemists.jpg
	new file:   content/uploads/2011/09/RJ-and-Carter.jpg
	new file:   content/uploads/2011/09/eye.png
	new file:   content/uploads/2011/09/hacker-olive-oil-lamp-10-birds-eye.jpg
	new file:   content/uploads/2011/09/hacker-olive-oil-lamp-11-profile.jpg
	new file:   content/uploads/2011/09/hacker-olive-oil-lamp-12-birds-eye.jpg
	new file:   content/uploads/2011/09/hacker-olive-oil-lamp-2.jpg
	new file:   content/uploads/2011/09/hacker-olive-oil-lamp-3.jpg
	new file:   content/uploads/2011/09/hacker-olive-oil-lamp-4-materials.jpg
	new file:   content/uploads/2011/09/hacker-olive-oil-lamp-5-wick-and-holder.jpg
	new file:   content/uploads/2011/09/hacker-olive-oil-lamp-6-pickle-jar.jpg
	new file:   content/uploads/2011/09/hacker-olive-oil-lamp-7.jpg
	new file:   content/uploads/2011/09/hacker-olive-oil-lamp-8-bending-with-plyers.jpg
	new file:   content/uploads/2011/09/hacker-olive-oil-lamp-9-completed-holder.jpg
	new file:   content/uploads/2011/09/hacker-olive-oil-lamp.jpg
	new file:   content/uploads/2011/09/lecture-viewers-vs-doers.jpg
	new file:   content/uploads/2011/10/df_inode-day.png
	new file:   content/uploads/2011/10/df_inode-year.png
	new file:   content/uploads/2011/10/microsoft-resells-free-internet-services.png
	new file:   content/uploads/2011/10/skull.jpg
	new file:   content/uploads/2011/10/smile-guy.jpg
	new file:   content/uploads/2011/10/stack-vs-queue.png
	new file:   content/uploads/2011/10/wind.jpg
	new file:   content/uploads/2011/11/job-networking-chutes-and-ladders.gif
	new file:   content/uploads/2011/12/linkpeek-number-1-on-hacker-news.png
	new file:   content/uploads/2012/02/Screenshot-at-2012-02-16-172630.png
	new file:   content/uploads/2012/02/cutest-redsox-fan-ever.jpg
	new file:   content/uploads/2012/02/ice-king-adventure-time.png
	new file:   content/uploads/2012/02/jake-standing.png
	new file:   content/uploads/2012/02/lump-princess.png
	new file:   content/uploads/2012/02/mypaint-finn-adventure-time.png
	new file:   content/uploads/2012/02/mypaint-jake-adventure-time.png
	new file:   content/uploads/2012/02/sad-jake.png
	new file:   content/uploads/2012/02/wireshark-filter.png
	new file:   content/uploads/2012/02/wireshark-packets.png
	new file:   content/uploads/2012/02/wireshark-sniff.png
	new file:   content/uploads/2012/02/wireshark-start-capture.png
	new file:   content/uploads/2012/02/wireshark-stop-capture.png
	new file:   content/uploads/2012/02/wireshark.png
	new file:   content/uploads/2012/03/3247325203_6108897833_o.jpg
	new file:   content/uploads/2012/03/6876068965_db8a5d12b2_o.jpg
	new file:   content/uploads/2012/03/6876069657_5a2f4ae487_o.jpg
	new file:   content/uploads/2012/03/6876071849_b82bca1076_o.jpg
	new file:   content/uploads/2012/05/high-load-after-updating-ubuntu-from-10.04-LTS-to-12.04-LTS.png
	new file:   content/uploads/2012/05/ubuntu.12.04.swap_.year_.png
	new file:   content/uploads/2012/06/dedication-eye-chemical-burn.jpg
	new file:   content/uploads/2012/06/vertical-scale-marked.png
	new file:   content/uploads/2012/06/vertical-scale.png
	new file:   content/uploads/2012/11/virt-back-kvm-xen-virtualbox.png
	new file:   content/uploads/2012/12/comments-docs-tests-oh-my.xcf_.png
	new file:   content/uploads/2012/12/commit-working-code.xcf_.png
	new file:   content/uploads/2012/12/follow-the-leader.xcf_.png
	new file:   content/uploads/2012/12/holiday-postal-receipt-virus.png
	new file:   content/uploads/2012/12/ideas-blog-get-heard.xcf_.png
	new file:   content/uploads/2012/12/less-is-more.xcf_.png
	new file:   content/uploads/2012/12/pull-request-stay-focused.xcf_.png
	new file:   content/uploads/2012/12/spot-the-diff.xcf_.png
	new file:   content/uploads/2012/12/start-small.xcf_.png
	new file:   content/uploads/2013/03/2013-03-02-scale-virt-back-domfetcher-scaled.png
	new file:   content/uploads/2013/03/2013-03-03-explaining-cache-scaled.png
	new file:   content/uploads/2013/03/2013-03-22-tornado-callback-long-polling-event-loop-scaled.png
	new file:   content/uploads/2013/04/baby-monkey-fxhp.jpg
	new file:   content/uploads/2013/06/image.jpeg
	new file:   content/uploads/2013/06/minecraft-vs-craftbukkit.png
	new file:   content/uploads/2013/07/config-mangement-vs-remote-execution-2.png
	new file:   content/uploads/2013/07/config-mangement-vs-remote-execution.png
	new file:   content/uploads/2013/10/mysza.gif
	new file:   content/uploads/2013/11/rss-icon-25-x-25.png
	new file:   content/uploads/2014/03/download_icon-40px.png
	new file:   content/uploads/2014/09/2014-09-17-reality-shattered-150x150.png
	new file:   content/uploads/2014/09/2014-09-17-reality-shattered-212x300.png
	new file:   content/uploads/2014/09/2014-09-17-reality-shattered-726x1024.png
	new file:   content/uploads/2014/09/2014-09-17-reality-shattered.png
	new file:   content/uploads/2014/09/2014-09-18-rabbits-150x150.png
	new file:   content/uploads/2014/09/2014-09-18-rabbits-177x300.png
	new file:   content/uploads/2014/09/2014-09-18-rabbits.png
	new file:   content/uploads/hotlink.jpg
2016-06-03 11:22:15 -04:00

90 lines
3.3 KiB
ReStructuredText

How to overload default function arguments in python using lambda
#################################################################
:date: 2013-01-12 11:22
:author: Russell Ballestrini
:tags: Code, Greatest Hits, Guide
:slug: how-to-overload-default-function-arguments-in-python-using-lambda
:status: published
Python Lambda functions are very powerful but I often forget how they
work or the fun things they do. This post will document how to use a
lambda to provide different default arguments to a function.
We will use the `human function found in
ago.py <https://bitbucket.org/russellballestrini/ago/overview>`__ as an
example - because I'm the module author and I really like it. Lets use
the interactive python interpreter to run help on the human function.
::
>>> from ago import human
>>> help( human )
Help on function human in module ago:
human(dt, precision=2, past_tense='{} ago', future_tense='in {}')
Accept a datetime or timedelta, return a human readable delta string
.. raw:: html
</p>
Shown above the human function accepts 1 argument and 3 named keyword
arguments. The dt argument must be a datetime or timedelta object, the
precision must be an integer, and the other two must be strings. If we
didn't like the default arguments, we would need to specify (or pass in)
new values each time we invoked the function.
Example: ``human(dt, 3, 'this happened {} ago!', 'in {} from now!')``.
If we know we will always want different default arguments we can create
a lambda function to shorten the invocation length.
::
>>> h = lambda dt : human(dt, 3, 'this happened {} ago!', 'in {} from now!')
>>> print h( dt ) # h is much shorter then human, and still reusable!
| Above creates a new function h who only accepts one argument dt. This
function calls human with our default arguments.
| This lambda is equivalent to this regular python function:
::
>>> def h( dt ):
... return human(d, 3, 'this happened {} ago!', 'in {} from now!')
>>> print h( dt ) # h is much shorter then human, and still reusable!
Here is a working example to show the new lambda function h in action:
::
>>> from datetime import datetime
>>> from datetime import timedelta
>>> from ago import human
>>>
>>> h = lambda dt : human(dt, 3, 'this happened {} ago!', 'in {} from now!')
>>>
>>> present = datetime.now()
>>>
>>> for i in range( 1, 15 ):
... if i % 2 == 0:
... new_date = present - timedelta( i, i * i, i * i * i )
... else:
... new_date = present + timedelta( i, i * i, i * i * i )
... h( new_date )
...
'in 22 hours, 9 minutes, 30 seconds from now!'
'this happened 2 days, 1 hour, 50 minutes ago!'
'in 2 days, 22 hours, 9 minutes from now!'
'this happened 4 days, 1 hour, 50 minutes ago!'
'in 4 days, 22 hours, 9 minutes from now!'
'this happened 6 days, 1 hour, 51 minutes ago!'
'in 6 days, 22 hours, 10 minutes from now!'
'this happened 8 days, 1 hour, 51 minutes ago!'
'in 8 days, 22 hours, 10 minutes from now!'
'this happened 10 days, 1 hour, 52 minutes ago!'
'in 10 days, 22 hours, 11 minutes from now!'
'this happened 12 days, 1 hour, 52 minutes ago!'
'in 12 days, 22 hours, 12 minutes from now!'
'this happened 14 days, 1 hour, 53 minutes ago!'