database - How to ensure that a python function generates its output based only on its input? -


The function usually uses only the values ​​of its arguments to generate an output. However, there are also cases where the function reads something from the file system or from the database or from the web, to prepare its output, I would like a simple and reliable way to ensure that this is not something like that.

In a way, I see that creating a white list of python libraries which can be used to read from the file system, database or web but if this is the way to go, Potentially large) list can be found. In addition, I do not want to disable the entire library because it can be used to read from the file system. For example, I want users to be able to use Panda Library (to store and manipulate tabular data). I do not want them to use this library to read data from the file system.

Is there a solution to this problem?

The answer is not what you are looking for is such an act Which tests for functional accuracy but, as shown in this code, there is no way to guarantee that no side effects are actually being called.

  square fu (object): def __init__ (self, x): self. X = x def __add __ (auto, Y): print ("bad bad effects here here ...") # Just proceed to read a file and return the stuff. # It looks pure ... def f X): Return x + 1 # but actually ... & gt; & Gt; & Gt; F (Fu (1)) Hahaha bad side effects here ...   

Due to the wide variety of objects, they can redefine their behavior (field access, calling, operator overloading, etc.) , You can always pass such input which makes the pure function impure. That's why pure work is only those who do not really do anything with their arguments ... there is a class which is usually less useful.

Of course, if you can specify other restrictions, then it becomes easy.

Comments

Popular posts from this blog

java - ImportError: No module named py4j.java_gateway -

python - Receiving "KeyError" after decoding json result from url -

.net - Creating a new Queue Manager and Queue in Websphere MQ (using C#) -