python method variables and Objects created in method
<p>So I know that in Python variables declared in a method are not visible outside of it. But what is their life cycle? I would expect that a variable declared inside a method is created each time that method is called. </p>

<p>I'm asking because of the following situation. I have a method as follows(this is used with cherrypy, probably not relevant but still I'll add it for the off chance this influences it in any way).</p>

<p>First the custom Filter class I'm using:</p>

<pre><code>class Filter():
"""
Class used to filter tables displayed in UI, based on few criteria.
Initialize filter with a list of fields, and correspondinf values.
"""
def __init__(self, display_name= "", fields= [], values= [], operations= []):
    self.display_name = display_name
    self.fields = fields
    self.values = values
    self.selected = False 
    self.operations = operations
</code></pre>

<p>Now the method I'm talking about</p>

<pre><code>@cherrypy.expose
@logged() 
def getfiltereddatatypes(self, name, filters, datatype):
    ...some mumbo jumbo...

    default_filter = self._get_default_filters(inputTree, name)

    #default_filter here can be a object of type Filter if defined or None otherwise

    print "RECIEVED " + str(filters)
    if default_filter is not None:
        print "CREATING NEW"
        new_filter = copy.deepcopy(default_filter)
    else:
        print "CREATING NEW"
        new_filter = Filter()
    [new_filter.fields.append(value) for value in filters['fields']]
    [new_filter.operations.append(value) for value in filters['operations']]
    [new_filter.values.append(value) for value in filters['values']]
    print "LENGTH =" + str(len(new_filter.fields))

    ...Some other mumbo jumbo....
</code></pre>

<p>So basically I want to add the filter values recieved as parameter in the <code>filters</code> variable to the default_ones. But i want this to be done each time the method is called, or to better explain it, I made the prints to show my problem. So at first method call:</p>

<pre><code>    RECIEVED {'operations': ['!='], 'fields': ['model.DataType.subject'], 'values': ['w']}
    CREATING NEW
    LENGTH =1
</code></pre>

<p>So as expected, a new Filter object is created, and the operations, fields, and values from the recieved parameter are added, length(fields) = 1. But at my second pass:</p>

<pre><code>    RECIEVED {'operations': ['!='], 'fields': ['model.DataType.subject'], 'values': ['']}
    CREATING NEW
    LENGTH =2
</code></pre>

<p>Now this I can't explain this behaviour. As you can see, the CREATING NEW brach of the if is called again, the <code>filters</code> variable holds one item lists but the length of the result is 2. If I run it again and again it will always increment. It's like the <code>new_filter = Filter()</code> call returns the new_filter from the previous method call. What causes this kind of behaviour? I'm thiking maybe if I do a <code>new_filter = copy.deepcopy(Filter())</code> it might solve it but why should I be forced to do that?</p>

<p>Regards,
Bogdan </p>
