【问题标题】:Mutating / in-place function invokation or adaptation in PythonPython中的变异/就地函数调用或适应
【发布时间】:2015-07-08 14:43:00
【问题描述】:

我的代码中经常有执行以下操作的语句:

long_descriptive_variable_name = some_function(long_descriptive_variable_name)

这很清楚,但同时又冗长且有些多余。 Python中是否有任何方法可以通过使some_function充当“变异”(“就地”)函数来简化此语句?

例如,在 Julia one can often do 中:

some_function!(long_descriptive_variable_name)

这被分派到直接写入long_descriptive_variable_namesome_function 版本,有效地更新了变量。

对于泛型函数some_function,有什么方法可以在 Python 中简明扼要地表达相同的意思吗?

如果对通用对象方法做同样的事情呢?即简化

long_variable_name = long_variable_name.method(arg1, arg2)

如果当前版本的 Python 无法(轻松)实现上述操作,是否有任何 PEP 考虑在不久的将来进行此更改?

【问题讨论】:

  • 我认为此类任务没有直接的方法,但您可以在函数内创建一个global 变量并将返回结果分配给该变量。

标签: python python-3.x in-place mutating-function


【解决方案1】:

您所要求的可以这样实现,但我当然不建议这样做:

>>> x = 10
>>> def foo(func, string_of_var):
    globals()[string_of_var] = func(globals()[string_of_var])

>>> def bar(x):
    return x * 2

>>> foo(bar, 'x')
>>> x
20

就 PEP 而言,如果有的话,我怀疑它会被批准。调用像这样隐式更改值的函数违背了 Python 的禅宗:

The Zen of Python, by Tim Peters

Beautiful is better than ugly.
Explicit is better than implicit.  <==== Relevant line
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.  <==== also probably relevant
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.  <==== And this one for good measure
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!

这也可能需要大量的工作,而不会增加太多的语言。对于this reason,Python 没有++/--。当x += 1 实现同样的事情时,添加它会做更多的工作。在这里也是如此,调用函数时需要几个人的大量工作才能为您节省一些按键。

【讨论】:

    【解决方案2】:

    免责声明:这不应该用在任何严肃的代码中,我写它只是为了好玩,甚至不认为它很聪明。

    不确定这是否是你想要的(如果离题,请原谅我),但我在创作时真的很享受。

    class FancyThing(object):
    
        def __init__(self, value):
    
            self.value = value
    
        def update(self, callback):
    
            self.value = callback(self.value)
    
        def __get__(self, instance, owner):
    
            return instance.value
    
        def __set__(self, instance, value):
    
            instance.value = value
    
        def __str__(self):
    
            return str(self.value)
    
        def __add__(self, other):
    
            return self.value + other
    

    如果你在这个类中包装了一些东西,你可以update 使用任何随机回调。

    def some_func(val):
        return val * 3
    
    a = FancyThing(3.5)
    print a
    
    a.update(tester)
    print a
    b = a + 5
    print b
    

    输出:

    3.5
    10.5
    15.5
    

    有趣的是,您必须定义 很多 内置方法才能像使用普通方法一样使用内部值,就像我为 __add__() 所做的那样。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-07-16
      • 1970-01-01
      • 1970-01-01
      • 2017-05-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多