Enabling or adapting the mutating / in-place function in Python

I often have instructions in my code that do the following:

long_descriptive_variable_name = some_function(long_descriptive_variable_name) 

which is very clear, but verbose and somewhat redundant at the same time. Is there a way in Python to simplify this statement, perhaps by making some_function act as a mutating (in-place) function?

For example, in Julia you can often do the following:

 some_function!(long_descriptive_variable_name) 

and this is sent to the version of some_function , which is written directly to long_descriptive_variable_name , effectively updating the variable.

Is there a way to briefly express the same thing in Python for the common some_function function?

How to do the same with the shared object method? those. simplification

 long_variable_name = long_variable_name.method(arg1, arg2) 

If the above is not (easily) doable with current versions of Python, are there any PEPs considering this change in the near future?

+6
source share
2 answers

What you ask for can be achieved like this, but I certainly would not recommend doing this:

 >>> 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 

As for the PEP for changing it, if any, I doubt it will be approved. A call to a function that implicitly changes the value goes against Zen 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 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 do more of those! 

It is also likely to entail considerable work without adding much to the language. Python does not have ++ / -- for this reason . It would be more work to add it when x += 1 reaches the same. The same can be said about the fact that several people resort to several actions in order to save several keystrokes when calling functions.

+1
source

DISCLAIMER: this should not be used in any serious code, I wrote it just for fun and do not even consider it smart.

Not sure if this is what you wanted (so forgive me if it’s off topic), but I really enjoyed it by creating it.

 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 

If you wrap something in this class, you can update with any random callback.

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

Outputs:

 3.5 10.5 15.5 

The funny thing is that you have to define many built-in methods in order to be able to use the internal value, as usual, for example, for __add__() .

0
source

All Articles