acting on items passed to a method via a dictiomary

David M. Cooke cookedm+news at physics.mcmaster.ca
Mon Oct 18 21:27:47 CEST 2004


"Diez B. Roggisch" <deetsNOSPAM at web.de> writes:

>> 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:

Or the more readable (IMO):

def foo(items=None):
    if items is not None:
       ...

They're equivalent, but a reader could see 'not items is None' as
'(not items) is None)' [although it's not].

-- 
|>|\/|<
/--------------------------------------------------------------------------\
|David M. Cooke
|cookedm(at)physics(dot)mcmaster(dot)ca



More information about the Python-list mailing list