2024-10-05 14:25:36 发布
网友
可以使用python中的函数清空字符串吗?在
例如:
otherText="hello" def foo(text): text="" foo(otherText) print(otherText)
印刷品
hello
而不是空字符串。有没有一种方法可以在不指定返回值或使用全局变量的情况下清空字符串?在
正如zerkms所指出的,这是完全不可能的,python不通过引用传递参数。在
有一些技巧可以用作解决方法,例如传递包含字符串的列表或对象。在
otherText=["hello"] def foo(text): text[0]="Goodbye string" foo(otherText) print(otherText) //Goodbye string
这是不可能的。这有两个原因
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
正如zerkms所指出的,这是完全不可能的,python不通过引用传递参数。在
有一些技巧可以用作解决方法,例如传递包含字符串的列表或对象。在
这是不可能的。这有两个原因
Python字符串是不可变的
Python实现了一个所谓的"call by sharing" evaluation strategy:
相关问题 更多 >
编程相关推荐