What is a function parameter =[] for?

Ben Finney ben+python at benfinney.id.au
Wed Nov 18 20:26:28 EST 2015


MRAB <python at mrabarnett.plus.com> writes:

> On 2015-11-19 00:34, fl wrote:
> > What does 'del' do? It does not look like a thorough list deletion
> > from the effect.
>
> No, "del list1" doesn't delete the list, it deletes the name "list1".
> The list still exists as the default parameter.

More generally, ‘del’ deletes a *reference*. Names are one kind of
reference; others include items in a collection such as a dict or set.

Deleting a reference *never* affects the value referred to.

However, once a value has no more references, the program can no longer
access it, so the Python machine is at liberty to delete the value
behind the scenes at some future point.

> A default parameter is evaluated when the function is defined, and
> that value exists as the default as long as the function exists.

So, because the function retains a reference to the value, the value
remains unaffected when you delete the reference ‘list1’.

-- 
 \         “True greatness is measured by how much freedom you give to |
  `\      others, not by how much you can coerce others to do what you |
_o__)                                               want.” —Larry Wall |
Ben Finney




More information about the Python-list mailing list