Get classes from "self.MyClass" to improve subclassability

Thomas Güttler hv at tbz-pariv.de
Fri Jun 12 07:12:52 EDT 2015


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, I have seen the "namespace" kwarg.

This is not the first time I would like the upstream code to make subclassing more fun.

Some months ago I asked myself how to call the "self.Namespace()" pattern:
 
http://stackoverflow.com/questions/27571848/name-of-design-pattern-get-class-from-class-level

The answer with the most upvotes states it is the Factory method pattern.

I prefer "self.Namespace()" to namespace kwargs.

What do you think? 



More information about the Python-list mailing list