A question on modification of a list via a function invocation

Steve D'Aprano steve+python at pearwood.info
Wed Aug 16 23:23:05 EDT 2017


On Thu, 17 Aug 2017 12:24 pm, Stefan Ram wrote:

> Steve D'Aprano <steve+python at pearwood.info> writes:
>>On Thu, 17 Aug 2017 11:07 am, Stefan Ram wrote:
>>>So, when an argument is actually passed, then the parameter
>>>can be modified? It seems Mok-Kong Shen was writing about
>>>this case.
>>I would say that the argument is modified.
> 
>   I was thinking along these lines:
> 
>>>> def function( parameter ):
> ...     parameter = parameter + 1
> ...     print( parameter )
> ...
>>>> argument = 4
>>>> function( argument )
> 5
>>>> argument
> 4

Okay, you are talking about the *local variable* called "parameter". We can:

- mutate the *object* bound to that local variable;

- re-bind (reassign) a different object to that local variable.


I was talking about the *name* (an identifier) in the function declaration, a
literal piece of text appearing in your source code. In that sense, we can't do
anything to the parameter until the function is called and an argument is
passed in.

I see now that both ways of looking at this have some validity. The FAQ helps a
bit:

    Parameters are defined by the names that appear in a function
    definition, whereas arguments are the values actually passed
    to a function when calling it.

https://docs.python.org/3/faq/programming.html#faq-argument-vs-parameter


so in your example the argument is certainly the int 4, the parameter is the
name (the identifier) in the function declaration, but how do we refer to the
local variable inside the function? Is it the parameter (an identifier) or an
argument (an object) or something else?

I think the terminology is inconsistent. For example, here:

https://chortle.ccsu.edu/java5/Notes/chap34A/ch34A_3.html


"formal parameter" is defined as the *identifier* but later on the same page
seems to use it to refer to the *local variable*.

I think that if we are talking about the local variable when we say "parameter",
it is okay to talk about it being re-bound, but if we're talking about the
identifier, it is not.




-- 
Steve
“Cheer up,” they said, “things could be worse.” So I cheered up, and sure
enough, things got worse.




More information about the Python-list mailing list