是否可以使用 python 中的函数清空字符串?
例如:
otherText="hello"
def foo(text):
text=""
foo(otherText)
print(otherText)
打印
hello
而不是空字符串。有没有一种方法可以在不为其分配返回值或使用全局变量的情况下清空字符串?
请您参考如下方法:
这是不可能的。有两个原因
Python 字符串是不可变的
Python 实现了一个所谓的 "call by sharing" evaluation strategy :
The semantics of call by sharing differ from call by reference in that assignments to function arguments within the function aren't visible to the caller
