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 7603807
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T23:44:52+00:00 2026-05-30T23:44:52+00:00

Refer to this >>> def foo(counter=[0]): … counter[0] += 1 … print(Counter is %i.

  • 0

Refer to this

>>> def foo(counter=[0]):
...   counter[0] += 1
...   print("Counter is %i." % counter[0]);
... 
>>> foo()
Counter is 1.
>>> foo()
Counter is 2.
>>>

Default values are initialized only when the function is first evaluated, not each time it is executed, so you can use a list or any other mutable object to maintain static values.

Question> Why the counter can keep its updated value during different callings? Is it true that counter refers to the same memory used to store the temporary list of the default parameter so that it can refer to the same memory address and keep the updated values during the calls?

  • 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-30T23:44:54+00:00Added an answer on May 30, 2026 at 11:44 pm

    First things first: if you are coding in Python: forget about “memory address” – you will never need one. Yes, there are objects, and they are placed in memory, and if you are referring to the same object, it is in the same “memory address” – but that does not matter – there could even be an implementation where objects don’t have a memory address at all (just a place in a data structure, for example).

    Then, when Python encounters the function body, as it is defined above, it does create a code object with the contents of the function body, and executes the function definition line – resolving any expressions inlined there and setting the results of those expressions as the default parameters for that function. There is nothing “temporary” about these objects. The expressions (in this case [0]) are evaluated, the resulting objects (in this case a Python list with a single element) are created, and assigned to a reference in the function object (a position in the functions’s “func_defaults” attribute – remember that functions themselves are objects in Python.)

    Whenever you call that function, if you don’t pass a value to the counter parameter, it is assigned to the object recorded in the func_defaults attribute -in this case, a Python list. And it is the same Python list that was created at function parsing time.

    What happens is that Python lists themselves are mutable: one can change its contents, add more elements, etc…but they are still the same list.
    What the code above does is exactly incrementing the element in the position 0 of the list.

    You can access this list at any time in the example above by typing foo.func_defaults[0]
    If you want to “reset” the counter, you can just do: foo.func_defaults[0][0]=0, for example.

    Of course, it is a side effect of how thigns a reimplemented in Python, and though consistent, and even docuemnted, should not be used in “real code”. If you need “static variables”, use a class instead of a function like the above:

    class Foo(object):
      def __init__(self):
         self.counter = 0 
      def __call__(self):
        self.counter += 1 
        print("Counter is %i." % self.counter)
    

    And on the console:

    >>> foo = Foo()
    >>> foo()
    Counter is 1.
    >>> foo()
    Counter is 2.
    >>> 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In clojure I have lines like this that define default values: (def *http-port* 8080)
I encountered a new error/bug I have not seen before. What does this refer
I have 2 files: xxxxxTest.java [ refer this ] public class xxxxxTest extends TestCase
Please refer to this post. I have become able to configure my web.config file
Please refer to this background question. After constructing this COUNT, how would I then
I want to refer to this post, because it might relate: Make Form Fields
What does 'this' keyword refer to when used in global object? Let's say for
Note: In this question I'm using the term autocomplete (or iterative search) to refer
In JavaScript, the this operator can refer to different things under different scenarios. Typically
In DOM, is it OK to refer to an element's attributes like this: var

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.