我的代码中经常有执行以下操作的语句:
long_descriptive_variable_name = some_function(long_descriptive_variable_name)
这很清楚,但同时又冗长且有些多余。Python中是否有任何方法可以通过将 some_function
其作为“变异”(“就地”)函数来简化此语句?
例如,在 Julia中,通常可以执行以下操作:
some_function!(long_descriptive_variable_name)
这被分派到some_function
直接写入的版本long_descriptive_variable_name
,有效地更新了变量。
有什么方法可以在 Python 中为泛型函数简明扼要地表达相同的意思some_function
吗?
用通用对象方法做同样的事情怎么样?即简化
long_variable_name = long_variable_name.method(arg1, arg2)
如果上述内容在当前版本的 Python 中无法(轻松)实现,是否有任何 PEP 考虑在不久的将来进行此更改?