April 13, 2008

Some 'good oil' on python default arguments

Traps and a simple way to avoid-em for python using mutable types as default arguments - well worth a read

http://boodebr.org/main/python/tourist/mutable-obj-as-default-arg
"Many modern languages allow for default function arguments. Default arguments can be a great thing - they allow you to use sensible defaults for the common cases, without taking away the power to add more functionality as needed. You can arbitrarily expand a function definition without breaking existing code by providing sensible defaults for the new parameters.

Here is a fun example of how Python will bite you if you try to use a mutable object (like a list, dict, or object instance) as a default argument. The following is an example function I'm trying to write ..."

0 comments:

Post a Comment

About Me

Wellington, New Zealand
Software Architect / Developer with a keen interest in open data and applications of the Semantic Web.