Check that Python function does not modify argument?

2024/11/19 9:23:25

You know how in Python, if v is a list or a dictionary, it's quite common to write functions that modify v in place (instead of just returning the new value). I'm wondering if it is possible to write a checker that identifies such functions.

For simplicity, say you have a function f, which only takes one argument - a and returns in finite time (the return value is actually irrelevant). Assume also that for any input value v, f(v) always does the same thing (i.e. the logic inside of f does not depend on any context or environment values - it's a pure computation on a).

Is it possible to write a function m, such that m(f, v) returns True if and only if f(v) actually changes the original value of v?

Answer

No; this is equivalent to the Halting problem. If such an m did exist, then I could just write:

def f(a):if m(f, a):return aelse:# Modify `a` somehowreturn a

and we get a contradiction.

https://en.xdnf.cn/q/118557.html

Related Q&A

What Python 3 version for my Django project

I will try to port my Python 2.7 with Django to Python 3. But now my question is what version is the most stable one today? Ive heard people use 3.2 and 3.4 and recommend it. But now Im asking you guy…

Error during runfile in Eclipse with PyDev/ error initializing console

Using a PyDev console in Eclipse, which initially worked fine. Python code would work inside the console. When I started writing a file within a PyDev module, I tried executing runfile() but the consol…

Reversibly encode two large integers of different bit lengths into one integer

I want to encode two large integers of possibly different maximum bit lengths into a single integer. The first integer is signed (can be negative) whereas the second is unsigned (always non-negative). …

Pytube AttributeError: NoneType object has no attribute span

Hi I have a problem with AttributeError: NoneType object has no attribute span I read on the StackOverflow a channel with this problem on this I found the potential solution but it still not working he…

GDB: ModuleNotFoundError: No module named _tkinter [duplicate]

This question already has answers here:Why does tkinter (or turtle) seem to be missing or broken? Shouldnt it be part of the standard library?(4 answers)Closed last year.So Im trying to debug my C co…

Why can the difference of different floating numbers be 0 in python? [duplicate]

This question already has answers here:Float to Int type conversion in Python for large integers/numbers(2 answers)Closed last year.Why is the result of below code 0 in python3? a = "4.1512940685…

twinx and sns.barplot seaborn are overlapping bars

I would like to use sns.seaborn to display the np.sum and the np.mean on 2 different axes (with ax2 = ax1.twinx() I assume). The probem I have is that the graphs are overlapped and not readable.Am I ap…

Cant get tensorflow on Anaconda [duplicate]

This question already has an answer here:TensorFlow Importing error ( Using Anaconda)(1 answer)Closed 2 years ago.I need tensorflow to run a guided project I did on Coursera, but I am not able to insta…

Extract number between text and | with RegEx Python

I want to extract the information between CVE and |, but only the first time that CVE appear in the txt. I have now the follow code:import re f = open (/Users/anna/PycharmProjects/extractData/DiarioOfi…

How to reset a loop that iterates over a set?

How can I reset a loop that iterates over a set? A common answer for iterating over a list is to reset the index you are using to access the list, however sets do not support indices. The point is to …