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.
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
  Checking the number of arguments a function takes Chirumer 3 208 Jul-06-2021, 04:56 PM
Last Post: Chirumer
  Get anything before arguments Robotguy 3 406 May-19-2021, 01:16 AM
Last Post: BashBedlam
  TypeError: max_value() missing 2 required positional arguments: 'alpha' and 'beta' Anldra12 2 1,711 May-15-2021, 04:15 PM
Last Post: Anldra12
  Possible to dynamically pass arguments to a function? grimm1111 2 493 Feb-21-2021, 05:57 AM
Last Post: deanhystad
  Why Pass Functions as arguments? muzikman 14 1,481 Jan-18-2021, 12:08 PM
Last Post: Serafim
  "'DataFrame' objects are mutable, thus they cannot be hashed" Mark17 1 1,777 Dec-25-2020, 02:31 AM
Last Post: tinh
  Default values of arguments with the same id DrVictor 3 549 Dec-02-2020, 03:27 PM
Last Post: deanhystad
  Can property getters and setters have additional arguments? pjfarley3 2 638 Oct-30-2020, 12:17 AM
Last Post: pjfarley3
  Class Takes No Arguments horuscope42 4 1,920 Oct-26-2020, 11:10 PM
Last Post: not_username1234
  lists as arguments vinci 2 515 Oct-26-2020, 05:44 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

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