Short if

David Bolen db3l at fitlinxx.com
Wed Jul 7 17:15:00 EDT 2004


Peter Hansen <peter at engcorp.com> writes:

> That is true of course, but keep in mind also that the most common
> use of such patterns is actually to initialize self.foo to an
> *empty* mutable of the appropriate type.  In that case (and that
> case alone), it is generally equivalent at least in outcome, as
> if the user passes in an empty (say, list), the above code will
> use the "someDefaultMutable" empty list instead, but the result
> is still what was desired in most cases.

Maybe, maybe not.  Using an alternate empty list is only equivalent if
the function is not supposed to mutate the supplied parameter, but is
instead, for example, returning a reference to the resultant mutable
object.  (The original sample didn't include the full function so I
don't know if it was returning the reference).

Given that a mutable object is coming in as a parameter, and an
optional parameter at that, I think it's probably even odds that if it
was supplied, the function may be expected to mutate the object that
the caller supplied.

> But it is good to know the distinctions, and the fact that there
> are such distinctions still means that good ol' "if/else" is still
> the safest way to go...

Yep.

-- David



More information about the Python-list mailing list