On Fri, 12 Jun 2015 04:12:52 -0700, Thomas Güttler wrote:

> Here is a snippet from the argparse module:
> 
> {{{
>     def parse_known_args(self, args=None, namespace=None):
>         ...
>         # default Namespace built from parser defaults if namespace is
>         None:
>             namespace = Namespace() # < ======= my issue
> }}}
> 
> I subclass from the class of the above snippet.
> 
> I would like to use a different Namespace class.
> 
> if the above could would use
> 
>     namespace = self.Namespace()
> 
> it would be very easy for me to inject a different Namespace class.

Yes it would.

And here is how you do it, even when the parent class doesn't:

    class MySubclass(ParentClass):
        Namespace = Namespace
        def parse_known_args(self, args=None, namespace=None):
            if namespace is None:
                namespace = self.Namespace()
            # any other method overriding needed
            return super().parse_known_args(args, namespace)

In Python 2, you cannot use super(), you have to explicitly provide the 
arguments:

            return super(MySubclass,self).parse_known_args(args,namespace)


-- 
Steve
-- 
https://mail.python.org/mailman/listinfo/python-list

Reply via email to