Python Forum
Functions (Arguments Passing,Changing a mutable ,Assignment to Arguments Names) - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Functions (Arguments Passing,Changing a mutable ,Assignment to Arguments Names) (/thread-2265.html)



Functions (Arguments Passing,Changing a mutable ,Assignment to Arguments Names) - Adelton - Mar-02-2017

Hi there ,

I am a newbie to programming and I am learning user defined functions.I am having particular difficulty with the functions below which relate to the caller in a series of functions.Your help would be much appreciated.

1.
x = [1, 2, 3]
      def func(x):
       x[1] = 42 
       print(func(x)) # prints none 
       print(x)          #prints[1,42,3]
I am right in saying that this function has no variables and x is being passed temporarily  in this function because when func(x) is printed it returns none .The list changes because it is mutable.

2.   
x = 4 
    def func(y):
         print(y)  #  prints 4 
       print(func(x)) print none
Is this similar to example 1 where x is being passed in func(y) and y has not variable.An object Y is created which point to 4 temporarily .


3.
x= 3
  def func(x):
    x = 7
   print (func(x)) # prints none
   print(x)           # prints 3 
is this similar to the others in that this func(x) has not variable and executes none .


RE: Functions (Arguments Passing,Changing a mutable ,Assignment to Arguments Names) - metulburr - Mar-02-2017

a function always returns None by default. So if you print a function call, unless you return something , it will always be None

Its also hard to see currently as what is in what block as you have not used code tags
EDIT:
nevermind, your whole indentation is messed up too. Please fix your indentation. And use code tags in the future.


RE: Functions (Arguments Passing,Changing a mutable ,Assignment to Arguments Names) - zivoni - Mar-02-2017

Disclaimer: its only my very vague interpretation

I think that you are wrong when assuming "func has no variable". All of your functions have a local variable x. x  in a function body is not same as x outside the body of the function - in a local scope (inside the function) x denotes function's x and not outside one (when you use same names for variables in different scopes, a variable in inner scope "shadows" outside one and it gets confusing ...).

Variables in python are passed by assignment, your outside x references to some object and that reference is "copied" to your local x. A function cannot change outside x's reference - it will point to the same object regardless of function's trying. But if that object is mutable, the function can modify it.

The identity of the variable (address) can be obtained with built-in function id(), so its possible to "follow" what is happening.

Example function - same as your first one, with print to show variable's id.
def func(local_x):
    print("local_x points to: {}, its value is: {}".format(id(local_x), local_x))
    local_x[1] = 42
    print("local_x points to: {}, its value is: {}".format(id(local_x), local_x))
And test run:
Output:
>>> x = [1, 2, 3] >>> id(x) 139841538326408 >>> func(x) local_x points to: 139841538326408, its value is: [1, 2, 3] local_x points to: 139841538326408, its value is: [1, 42, 3] >>> x [1, 42, 3] >>> id(x) 139841538326408
You can see that both x and local_x always referenced the same object and that object was modified.

Things are little different when an object is immutable (cant be "changed").

Example func, same as your second one, again with print to show variable's id.
def func_imm(local_x):
    print("local_x points to: {}, its value is: {}".format(id(local_x), local_x))
    local_x = 7
    print("local_x points to: {}, its value is: {}".format(id(local_x), local_x))
And test run:
Output:
>>> x = 3 >>> id(3) 94008579423936 >>> func_imm(x) local_x points to: 94008579423936, its value is: 3 local_x points to: 94008579424064, its value is: 7 >>> x 3 >>> id(x) 94008579423936
local_x referenced same object (3) as outside x till assignment. Integers are immutable, the assignment could not change that object value, so instead of changing object it changed local_x's reference to object that represented 7. x still referenced 3 and as the function did not return local_x, it got "lost".