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

  • SEARCH
  • Home
  • 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 6351551
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T22:01:49+00:00 2026-05-24T22:01:49+00:00

Say you have a function that needs to maintain some sort of state and

  • 0

Say you have a function that needs to maintain some sort of state and behave differently depending on that state. I am aware of two ways to implement this where the state is stored entirely by the function:

  1. Using a function attribute
  2. Using a mutable default value

Using a slightly modified version of Felix Klings answer to another question, here is an example function that can be used in re.sub() so that only the third match to a regex will be replaced:

Function attribute:

def replace(match):
    replace.c = getattr(replace, "c", 0) + 1
    return repl if replace.c == 3 else match.group(0)

Mutable default value:

def replace(match, c=[0]):
    c[0] += 1
    return repl if c[0] == 3 else match.group(0)

To me the first seems cleaner, but I have seen the second more commonly. Which is preferable and why?

  • 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-24T22:01:50+00:00Added an answer on May 24, 2026 at 10:01 pm

    I use closure instead, no side effects.

    Here is the example (I’ve just modified the original example of Felix Klings answer):

    def replaceNthWith(n, replacement):
        c = [0]
        def replace(match):
            c[0] += 1
            return replacement if c[0] == n else match.group(0)
        return replace
    

    And the usage:

     # reset state (in our case count, c=0) for each string manipulation
     re.sub(pattern, replaceNthWith(n, replacement), str1)
     re.sub(pattern, replaceNthWith(n, replacement), str2)
     #or persist state between calls
     replace = replaceNthWith(n, replacement)
     re.sub(pattern, replace, str1)
     re.sub(pattern, replace, str2)
    

    For mutable what should happen if somebody call replace(match, c=[])?

    For attribute you broke encapsulation (yes i know that python didn’t implemented in classes from diff reasons …)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Lets say I have a function that needs to return some integer value. but
Say I have two functions that expect ...rest parameters private function a(...myParams):void { trace(myParams.length);
I have a C# function that does some processing and needs to return figures
I have a function that needs quite some internal temporary storage for its computations
I have a function that needs to call some functions sequentially. First, a fadeOut
Let's say I have a function called MyFunction(int myArray[][]) that does some array manipulations.
Say I have a function that takes an arbitrary number of arguments (the last
Let's say you have a function that returns a date: Date myFunc(paramA, paramB){ //conditionally
Say I have a Python function that returns multiple values in a tuple: def
Say I have a function foo that I want to call n times. In

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.