Default paramater values with python

Posted by Drakonen on September 26, 2008 in Uncategorized | Short Link

Default argument values for functions are a nice shortcut in python. They save you from writing many wrapper functions and make the code more readable and also easier to use. They can be used wrong though:

>>> def foo(a=[]):
...     a.append('bar')
...     return a
...
>>> foo()
['bar']
>>> foo()
['bar', 'bar']

Like described in idiomatic python, using a value which is a refenence (like a list, dict or instance) can lead to odd results. This is because the arguments are evaluated at compile time, not at runtime when the function is called! Instead of creating an empty list as one (inexperienced) would suspect, the default value becomes a reference to a list.

This can be nasty when it is used like this:

>>> def bar(time=datetime.now()):
...     print time
...
>>> bar()
2008-09-26 12:32:23.598052
>>> bar()
2008-09-26 12:32:23.598052

Just like in idiomatic Python, this is a good fix:

>>> def bar(time=None):
...     if time == None:
...             time = datetime.now()
...     print time
...

>>> bar()
2008-09-26 12:35:02.258992
>>> bar()
2008-09-26 12:35:03.059305

Instead of the default argument datetime.now(), using None, and later setting the current time instead of None works fine!

Facebook Twitter Email

2 Comments

  • maze says:

    Personally, I like patterns like:

    def bar(time=None):
    time = time or datetime.now()

    Also, for dictionary items/default parameters, you can use:

    defaults = {
    ‘verbose’: True,
    ‘quickfox’: 42,
    }

    def bar(**options):
    settings = defaults
    settings.update(options)

    bar(quickfox=23)

    Just my piece of mind

  • JK says:

    You should really use the faster `is` to compare against `None`:

    if time is None:
    do_stuff()

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">

Copyright © 2005-2012 Draakwired All rights reserved.
The Shades theme, version 1.7, is a BuyNowShop.com creation.

Social links powered by Ecreative Internet Marketing