【python】面向对象的编程,除内部reassignment/multiprocessing之外传递给函数的可更改对象在函数内部会被修改

  1. reassignment: In Python, all entities are objects, and all arguments are passed by reference. This means that if you modify the object that an argument refers to within a function, the changes will be reflected outside the function. This is true for mutable objects like lists, dictionaries, and PyTorch tensors.

However, reassignment (like x = F.relu(layer(x))) creates a new reference, and does not modify the original object. When you do x = F.relu(layer(x)) in your forward function, you’re not modifying the tensor that x originally referred to, but rather creating a new tensor and assigning it to x.

Here’s an example to illustrate this:

def modify_list(x):
    x.append(1)  # This modifies the original list

def reassign_list(x):
    x = [1, 2, 3]  # "reassignment" This does not modify the original list

my_list = [0]
modify_list(my_list)
print(my_list)  # Outputs: [0, 1]

my_list = [0]
reassign_list(my_list)
print(my_list)  # Outputs: [0]

In the modify_list function, the append operation modifies the original list object, so the changes are reflected outside the function. But in the reassign_list function, the x = [1, 2, 3] operation does not modify the original list, but rather creates a new list and assigns it to x. So, the changes are not reflected outside the function.

This is the same principle that applies in your forward function. When you do x = F.relu(layer(x)), you’re not modifying the tensor that x originally referred to, but creating a new tensor and assigning it to x. So, the original tensor that you passed as x will not be changed.

  1. multi-threading vs. multiprocessing
    使用multi-threading时如果在并行的代码里对global并且mutable的对象有更改,注意在并行前对每个thread deepcopy一个对象,否则报错.

When you are using multi-threading, all threads have access to the same memory space, including global variables or objects passed as arguments to the function. So when you pass a mutable object like a list, dictionary, or a user-defined object to a function that’s being run in a separate thread, and you modify that object within the function, those changes will indeed be visible to all threads. That’s because they’re all referencing the same object in memory.

So, the object is shared among all threads in the sense that all threads are working with the same object in memory.

When it comes to using multiprocessing, things are different. Each process has its own separate memory space. Python uses a mechanism called ‘pickling’ to serialize the program’s state and copy it into the new process. If a mutable object is passed as an argument to a function that’s being run in a separate process, and it’s modified within the function, those changes won’t be visible to other processes, unless you’re using a shared memory object or a server process.

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值