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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T09:34:27+00:00 2026-06-17T09:34:27+00:00

I was looking at the source code for the built-in argparse._AppendAction , which implements

  • 0

I was looking at the source code for the built-in argparse._AppendAction, which implements the "append" action, and puzzled over the way it is implemented:

    def __call__(self, parser, namespace, values, option_string=None):
        items = _copy.copy(_ensure_value(namespace, self.dest, []))
        items.append(values)
        setattr(namespace, self.dest, items)

To break it down:

  • _ensure_value is like dict.setdefault for attributes. That is, if namespace has an attribute with the name self.dest then it is returned, if not it is set to [] and returned.
  • _copy.copy(x) returns just a shallow copy. When x is a list it is exactly like list(x) (but slower).
  • Then the item is appended to the copy of the list gotten from namespace.
  • Finally the self.dest attribute of namespace is overwritten with the copy, which should cause the old list to be garbage collected.

Why do it in such a roundabout and inefficient way, throwing away a whole list for each append? Why doesn’t this suffice?

    def __call__(self, parser, namespace, values, option_string=None):
        items = _ensure_value(namespace, self.dest, [])
        items.append(values)
  • 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-06-17T09:34:28+00:00Added an answer on June 17, 2026 at 9:34 am

    I’m not an expert in the implementation, so (disclaimer) this is really just a guess. With this implementation, the user can pass a list as a default=... in a call to add_argument without it being mutated within argparse. Perhaps this type of safety was desired by the developers for one reason or another.

    The inefficiency you mention really isn’t a big deal. It’s for parsing commandline arguments, so this function is likely only called 10’s of times per program under heavy usage.


    I’ve tested this and indeed, If I use the following script (where argparse_temp is simply argparse.py copied to the current directory so I can play with it):

    import argparse_temp as argparse
    
    lst = [1,2,3]
    parser = argparse.ArgumentParser()
    parser.add_argument('-l',default=lst,action='append')
    print parser.parse_args()
    print lst
    

    This prints (when called as: python test1.py -l 4):

    Namespace(l=[1, 2, 3, '4'])
    [1, 2, 3]
    

    with argparse as is, but:

    Namespace(l=[1, 2, 3, '4'])
    [1, 2, 3, '4']
    

    with your proposed change.

    If you print the action that is returned by add_argument, you get:

    _AppendAction(option_strings=['-l'], dest='l', nargs=None, const=None, default=[1, 2, 3, '4'], type=None, choices=None, help=None, metavar=None)
    

    Which is it conceivable that argparse depends on that action elsewhere in the implementation. (Notice that default has been mutated here as well).

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

Sidebar

Related Questions

I was looking at the source code for the Append function in the SeqModule
I was looking at the source code to the hasattr built-in function and noticed
I'm looking for a good source code example of a supervised neural network which
I'm looking for source code for a web based study group. I'd prefer something
I was looking a source code of a particular page of my project and
I'm looking for the source code of Sun's standard java compiler, javac . jdk1.6.0_07
I am looking though some source code from a third party and am repeatedly
I have been looking through the source code for the Flash-based Google TV example
I have been looking at the source code of the IronPython project and the
I have been looking at the source code of raphael.js and I see a

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.