Skip to main content
Join the official Python Developers Survey 2018 and win valuable prizes: Start the survey!

Work around mutable default arguments in function definitions

Project description

#########
staticargs
#########
**A clean way to work around mutable default arguments in python functions**

Get staticargs
=============

staticargs supports Python 2 and 3. Tests are run for 2.7 and 3.4

.. code-block:: shell

pip install staticargs

Run the tests
=============
You must install tox, then run:

.. code-block:: shell

git clone https://github.com/HurricaneLabs/staticargs.git
cd staticargs
tox

Example
--------

First view the problem we are going to solve:

.. code-block:: python

>>> def append_cat(cats=[]):
... cats.append("cat")
... return cats
...
>>> print(append_cat())
['cat']
>>> print(append_cat())
['cat', 'cat']
>>> print(append_cat())
['cat', 'cat', 'cat']
>>>

Well that's not good! Notice how even though we are appending "cat" only once, the number of items in the list returned gets bigger each time. Learn more about this problem and some other Python gotchas by watching this amazing presentation from PyCon 2015 (this specific problem is discussed at 8:04): https://youtu.be/sH4XF6pKKmk

staticargs solves this problem for you:

.. code-block:: python

>>> from staticargs import staticargs
>>> import random
>>>
>>> @staticargs
... def append_cat(cats=[]):
... #I love cats
... cats.append("cat")
... return cats
...
>>> print(append_cat())
['cat']
>>> print(append_cat())
['cat']
>>> print(append_cat())
['cat']
>>>
>>> @staticargs
... def store_dog(dogs={}):
... #Dogs are OK I guess
... dog_name = random.choice(["rufus", "muffins", "scooby"])
... dogs[dog_name] = "good boy"
... return dogs
...
>>> print(store_dog())
{'muffins': 'good boy'}
>>> print(store_dog())
{'scooby': 'good boy'}
>>> print(store_dog())
{'rufus': 'good boy'}
>>>

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Filename, size & hash SHA256 hash help File type Python version Upload date
staticargs-1.0.0rc3.tar.gz (2.7 kB) Copy SHA256 hash SHA256 Source None Apr 12, 2015

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN SignalFx SignalFx Supporter DigiCert DigiCert EV certificate StatusPage StatusPage Status page