Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6325505
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T16:54:30+00:00 2026-05-24T16:54:30+00:00

So I know that in Python variables declared in a method are not visible

  • 0

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.

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

First the custom Filter class I’m using:

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

Now the method I’m talking about

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

So basically I want to add the filter values recieved as parameter in the filters 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:

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

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:

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

Now this I can’t explain this behaviour. As you can see, the CREATING NEW brach of the if is called again, the filters 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 new_filter = Filter() call returns the new_filter from the previous method call. What causes this kind of behaviour? I’m thiking maybe if I do a new_filter = copy.deepcopy(Filter()) it might solve it but why should I be forced to do that?

Regards,
Bogdan

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T16:54:31+00:00Added an answer on May 24, 2026 at 4:54 pm
    def __init__(self, display_name= "", fields= [], values= [], operations= [])
    

    The lists are created only once. So if you modify them the next call will have the modified lists. If you do not want this behaviour, simply use None as the default value and add something like this:

    if fields is None:
        fields = []
    self.fields = fields
    

    Another solution would be creating a new list no matter what was passed:

    self.fields = list(fields)
    

    There’s also a detailed article about this behaviour at http://effbot.org/zone/default-values.htm


    By the way, your abuse of list comprehensions is not nice.

    [new_filter.fields.append(value) for value in filters['fields']]
    

    should be

    new_filter.fields += filters['fields']
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know that python has a len() function that is used to determine the
We know that Python provides a lot of productivity over any compiled languages. We
How to know, in Python, that the directory you are in is inside a
I know that |DataDirectory| will resolve to App_Data in an ASP.NET application but is
I don't know if this is an obvious bug, but while running a Python
I know that I can do something like $int = (int)99; //(int) has a
I know that default cron's behavior is to send normal and error output to
I know that you can insert multiple rows at once, is there a way
I know that the MsNLB can be configured to user mulitcast with IGMP. However,
I know that .NET is JIT compiled to the architecture you are running on

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.