Keep a structure "alive" in a function over iteration?
<p>I want to keep astructure saved over iterations of a function, in order to keep data to use it for later purpose. 
There is a simple way to do it, in using a global variable. </p>

<p>The thing is, anyone could access the variable. 
So, is there a way to create a variable accessing only by the function, but that would not be erased when running this function? </p>

<p>Ex : </p>

<pre><code>mylist = []

def test():
    global mylist

    if mylist:
        my_list.append(1)

    other_stuff, using my_list
</code></pre>

<p>Is exactly what I want to do, but my_list could be accessed by anyone else. </p>

<p>(ok I know, the example is completely dumb) </p>

<p>Sorry for the formulation, but I could not come with something simpler</p>

<p>EDIT : Ok, so with so different (and all interesting) solutions. I'll quickly explain the idea of what I want to do :)</p>

<p>Let's imagine I want to calculate is a number is a prime. This is usually computationnaly coslty. </p>

<p>So I got a is_prime(value) function, returning False or True. </p>

<p>value can be any number, but there are chances (in my case) that value takes several times the same value (litteraly ^^).</p>

<p>In this case, I could use a (not to long ) list of primes I have already found to quickly check and save computations. 
But my prime_list is no use in a function returning true/false.</p>

<p>So here is my question :). </p>

<p>Hope to clarify some minds (including me! ).</p>
