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 . , , , .