What can you call Pass By Reference?

According to this tutorial, Python uses the "Pass By Reference".

They then give the following example. On which planet is this "Pass By Reference"? It seems like a clear Pass By Value option to me.

Thoughts?

def changeme( mylist ):
   mylist = [1,2,3,4];
   print "Values inside the function: ", mylist
   return

mylist = [10,20,30];
changeme( mylist );
print "Values outside the function: ", mylist

This parameter list is local to the changeme function. Changing mylist inside a function does not affect mylist. The function does nothing, and finally, it will lead to the following result:

# Values inside the function:  [1, 2, 3, 4]
# Values outside the function:  [10, 20, 30]
+4
source share
7 answers

. . , " ".

" " " ", - , . CLU 1974 . , Python, Iota, Java ( ), Ruby, JavaScript, Scheme, OCaml, AppleScript . " " ; . , Java , Java , Ruby , Ruby call-by-reference, . , , , .. " ".

, ( ), , , , . , , ( ), , , , . , - .

+4

, . , , pass-in , , , .

, Python, , "unboxed" (non-object). , , "" , , -, . Floats, Boolean, .

"" Python , , , C. , .

, , , , , , (). :

some_digits_of_pi = [3, 1, 4, 1, 5, 9, 2, 7]

def extend_pi(x):
    x[-1] = 6
    x += [5, 3, 5, 9]

x . ( += list.extend.) x , , . some_digits_of_pi , , , .

x = [2, 7, 1, 8, 2, 8, 1, 8] , x. , , .

, (some_digits_of_pi ) . x, , x .

, , .. . ; , , , , . , , .

, int list, +=, , += int - , a list. += (), ( ).

+5

, . , , - .

, , python pass-by-value.

[...] , ( None, ). , , , . [...]

, , , , , . , , .. - , .

+3

mylist = [1,2,3,4] changeme() mylist , , , , . mylist

+1

, , , " ". , .

, , "". . , , "". , , , , . , , , , . , . , , .

. mylist , mylist . , [1,2,3,4] - mylist. . .

+1

@Amadan Python Call by Sharing.

, , .

:

def changeme( mylist ):
   print locals()  # will show mylist value in function.
   mylist = [1,2,3,4];
   print locals()  # will show mylist value in function.
   print globals()['mylist'] # will show global mylist value
   print "Values inside the function: ", mylist
   return

, mylist , .

0

Python, , Java, , () . . . " " , , - (.. - , ). , , . , , , , , .

?

, "", , . , :

my_int = 4
def pass_by_value(value):
    value += 1
    return value
print pass_by_value(my_int)
print my_int

:

5
4

, my_int . 4, , 5. , . , . .

my_list = [1,2,3]
def pass_by_value_list(li):
    li.append(4)
    return li
print pass_by_value_list(my_list)
print my_list

:

[1, 2, 3, 4]
[1, 2, 3, 4]

. my_list . , ! my_list !!!

. , "". , , , , , . C/++ . , , .

? 2.0:

my_int = 4
def pass_by_value(value):
    print "Address of parameter before += is: ", id(value)
    value += 1
    print "Address of parameter after += is: ", id(value)
    return value
print "Address of parameter outside of the function is: ", id(my_int)
pass_by_value(my_int)

, :

Address of parameter outside of the function is:  40592528
Address of parameter before += is:  40592528
Address of parameter after += is:  40592504

, += 40592528. , ! += "" ! , . - 40592504 ( 40592528, ). , += int , .

, 2.0:

my_list = [1,2,3]
def pass_by_value_list(li):
    print "Address of parameter before .append() is: ", id(li)
    li.append(4)
    print "Address of parameter after .append() is: ", id(li)
    return li
print "Address of parameter outside of the function is: ", id(my_list)
pass_by_value_list(my_list)

:

Address of parameter outside of the function is:  110841160
Address of parameter before .append() is:  110841160
Address of parameter after .append() is:  110841160

, . , ! , . append . my_list . append , , , . .

, :

my_list = [1,2,3]
def dont_change_the_list(li):
    li.append(4)
    li = []
    return li
print dont_change_the_list(my_list)
print my_list

[]
[1, 2, 3, 4]

, , , -, , . , li = [] li, . - "", , . , my_list .

: Python pass-by-value . , , , .

0

All Articles