This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author paul.j3
Recipients Gabriel Devenyi, Markus.Amalthea.Magnuson, SylvainDe, Yclept.Nemo, bethard, docs@python, michal.klich, paul.j3, r.david.murray
Date 2016-10-03.00:31:38
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <>
One thing that this default behavior does is allow us to append values to any object, just so long as it has the `append` method.  The default does not have to be a standard list.

For example, in another bug/issue someone asked for an `extend` action.  I could provide that with `append` and a custom list class

    class MyList(list):
        def append(self,arg):
            if isinstance(arg,list):
                super(MyList, self).append(arg)

This just modifies `append` so that it behaves like `extend` when given a list argument.

     parser = argparse.ArgumentParser()
     a = parser.add_argument('-f', action='append', nargs='*',default=[])
     args = parser.parse_args('-f 1 2 3 -f 4 5'.split())

produces a nested list:

     In [155]: args
     Out[155]: Namespace(f=[['1', '2', '3'], ['4', '5']])

but if I change the `default`: 

     a.default = MyList([])
     args = parser.parse_args('-f 1 2 3 -f 4 5'.split())

produces a flat list:

     In [159]: args
     Out[159]: Namespace(f=['1', '2', '3', '4', '5'])

I've tested this idea with an `array.array` and `set` subclass.
Date User Action Args
2016-10-03 00:31:40paul.j3setrecipients: + paul.j3, bethard, r.david.murray, docs@python, Yclept.Nemo, Markus.Amalthea.Magnuson, SylvainDe, michal.klich, Gabriel Devenyi
2016-10-03 00:31:40paul.j3setmessageid: <>
2016-10-03 00:31:40paul.j3linkissue16399 messages
2016-10-03 00:31:38paul.j3create