Python Forum
Functions (Arguments Passing,Changing a mutable ,Assignment to Arguments Names)
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Functions (Arguments Passing,Changing a mutable ,Assignment to Arguments Names)
#1
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 .
Reply
#2
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.
Recommended Tutorials:
Reply
#3
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".
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Problems passing arguments containing spaces to bash script and then on to python kaustin 6 359 Apr-03-2024, 08:26 PM
Last Post: deanhystad
  Passing writable arguments to functions. Assembler 11 936 Jan-15-2024, 11:32 PM
Last Post: sgrey
  mutable argument in function definition akbarza 1 473 Dec-15-2023, 02:00 PM
Last Post: deanhystad
  [split] Class takes no arguments bily071 2 635 Oct-23-2023, 03:59 PM
Last Post: deanhystad
  How do I handle escape character in parameter arguments in Python? JKR 6 1,140 Sep-12-2023, 03:00 AM
Last Post: Apoed2023
  calling external function with arguments Wimpy_Wellington 7 1,428 Jul-05-2023, 06:33 PM
Last Post: deanhystad
  Why doesn't calling a parent constructor work with arbitrary keyword arguments? PurposefulCoder 4 930 Jun-24-2023, 02:14 PM
Last Post: deanhystad
  Using subprocess to execute complex command with many arguments medatib531 5 1,847 Apr-27-2023, 02:23 PM
Last Post: medatib531
Question log.exception() without arguments in old Python versions? cthart 5 1,155 Nov-19-2022, 07:09 PM
Last Post: Gribouillis
  Error TypeError: output_type_handler() takes 2 positional arguments but 6 were given paulo79 1 1,935 Oct-17-2022, 06:29 PM
Last Post: paulo79

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020