在修改调用变量的函数内创建空python bytearray

在修改调用变量的函数内创建空python bytearray,python,Python,我想创建一个函数来修改输入参数。有时,我想将输入参数重新分配给一个空的bytearray,但要在调用级别修改该空的bytearray。例如: def modify(current_bytes, new_byte): current_bytes += new_byte # this modifies calling variable no problem if check_done(current_bytes): current_bytes = bytearray(

我想创建一个函数来修改输入参数。有时,我想将输入参数重新分配给一个空的bytearray,但要在调用级别修改该空的bytearray。例如:

def modify(current_bytes, new_byte):
    current_bytes += new_byte # this modifies calling variable no problem
    if check_done(current_bytes):
        current_bytes = bytearray()  # this does not modify higher level variable
    if check_error(current_bytes):
        current_bytes = new_byte # WILL THIS WORK? 
我认为原因是函数中的
current_bytes
现在指向一个新对象,它是一个空bytearray。但是,如何确保
当前_字节
变量始终在修改更高级别的变量


另外,如果我做了
current\u bytes=new\u byte
这也会修改更高级别的调用对象吗?

试试
current\u bytes[:]=一些新的值
current\u bytes[:]=b'
会由数组本身清空。这是可行的,但我不太清楚为什么。在阅读中,[:]创建一个浅拷贝。我不知道为什么一个肤浅的副本在这里有用。