acting on items passed to a method via a dictiomary

Bengt Richter bokr at oz.net
Sun Oct 17 17:50:19 EDT 2004


On Sun, 17 Oct 2004 19:33:32 +0200, "Diez B. Roggisch" <deetsNOSPAM at web.de> wrote:

>> However, there are no problems whatsoever with the issue you remark on,
>> as long as the method never alters the 'items' object.  As long as only
>> nonmutating methods get called on 'items', i.e., 'items' is practically
>> treated as "read-only", Donnal Walter's approach is just fine.  The
>> issue you remark on does deserve to be made, but it's also important to
>> understand when it does matter and when it doesn't.
>
>You are right of course, but the high frequency of postings regarding
>"strange default value behaviour" made me want to make this point before
>Donnal steps into that pitfall. And while the example at hand didn't alter
>the contents of items, I'd nevertheless settled for items=None and used
>something like this:
>
>def foo(items=None):
>    if not items is None:
>        ....

I like to use the idiom

    def foo(items=None):
        if items is None: items = {} # or whatever non-shared mutable
        ...

Regards,
Bengt Richter



More information about the Python-list mailing list