Python按值调用和按引用调用

我正在尝试不同的python数据结构。似乎当我们使用列表,字典或任何其他自定义对象时,调用任何更改该对象的方法时,该引用都通过引用传递。但是,一切都是python中的对象,那么为什么不更改int和string呢?

python如何决定通过引用或按值传递哪个?像在C或C ++中一样,我们可以显式告诉是按值传递还是按引用传递。 python解释器如何知道何时使用哪个?

这是我正在尝试的一些代码

class Car(object):
    def __init__(self, color):
        self.color = color

def change_car_color(car):
    car.color = 'blue'
    return car

class LinkedList(object):
    def __init__(self, value):
        self.value = value
        self.next = None

def print_linked_list(ll):
    elements = []
    while(ll):
        elements.append(str(ll.value))
        ll = ll.next
    return "->".join(elements)

def change_linked_list_element_values(ll):
    while(ll):
        ll.value = -1
        ll = ll.next
    return ll

def reverse_linked_list(ll):
    current = ll
    neXt = ll.next
    current.next = None
    while(neXt):
        temp = neXt.next
        neXt.next = current
        current = neXt
        neXt = temp
    return current


def change_int(number):
    number = -1
    return number

def change_string(name):
    name = "this string has been modified"
    return name

def change_list(python_list):
    for i in range(len(python_list)):
        python_list[i] = -1
    return python_list

def change_dict(python_dict):
    for i in python_dict:
        python_dict[i] = -1
    return python_dict


#--------->>int object
num = 1
change_int(num) # chnages num to -1
print(num) # print 1

#--------->>string object
name = "I love python not the programming language but the reptile!"
change_string(name) # changes it to 'this string has been modified'
print(name) # print 'I love python not the programming language but the reptile!'


#--------->>list object
list_object = [1,2,3,4,5]
change_list(list_object) # changes it to [-1, -1, -1, -1, -1]
print(list_object) # prints [-1, -1, -1, -1, -1]

#---------->>dictionary object
dict_object = {'first':1, 'second':2, 'third':3, 'fourth':4, 'fifth':5}
change_dict(dict_object) # changes it to {'first': -1, 'second': -1, 'third': -1, 'fourth': -1, 'fifth': -1}
print(dict_object) # print {'first': -1, 'second': -1, 'third': -1, 'fourth': -1, 'fifth': -1}


#----------->>custom car object
car = Car('red')
print(car.color) # print red
change_car_color(car) # chnages it to blue
print(car.color) # print blue

#----------->>custom linked list
linked_list = LinkedList(1)
linked_list.next = LinkedList(2)
linked_list.next.next = LinkedList(3)
linked_list.next.next.next = LinkedList(4)
linked_list.next.next.next.next = LinkedList(5)

cp_linked_list = linked_list # copy of the linked list


print("before modification 1: ", print_linked_list(linked_list)) # prints before modification:  1->2->3->4->5
print("before modification 2: ", print_linked_list(cp_linked_list)) # prints before modification 2:  1->2->3->4->5

change_linked_list_element_values(linked_list) # change all element to -1
print("after modification 1: ", print_linked_list(linked_list)) # prints after modification 1:  -1->-1->-1->-1->-1
print("after modification 1: ", print_linked_list(cp_linked_list)) # prints after modification 1:  -1->-1->-1->-1->-1

change_linked_list_element_values(cp_linked_list) # change all element to -1
print("after modification 2: ", print_linked_list(cp_linked_list)) # prints after modification 2:  -1->-1->-1->-1->-1


# reinitializing linked list
linked_list = LinkedList(1)
linked_list.next = LinkedList(2)
linked_list.next.next = LinkedList(3)
linked_list.next.next.next = LinkedList(4)
linked_list.next.next.next.next = LinkedList(5)

print(print_linked_list(linked_list)) # print 1->2->3->4->5
reversed_linked_list = reverse_linked_list(linked_list) # reverses the linked list

print(print_linked_list(linked_list)) # print 1
print(print_linked_list(reversed_linked_list)) # print 5->4->3->2->1
评论