Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #92510 > unrolled thread

Get classes from "self.MyClass" to improve subclassability

Started byThomas Güttler <hv@tbz-pariv.de>
First post2015-06-12 04:12 -0700
Last post2015-06-15 06:14 -0700
Articles 6 — 4 participants

Back to article view | Back to comp.lang.python


Contents

  Get classes from "self.MyClass" to improve subclassability Thomas Güttler <hv@tbz-pariv.de> - 2015-06-12 04:12 -0700
    Re: Get classes from "self.MyClass" to improve subclassability Chris Angelico <rosuav@gmail.com> - 2015-06-12 21:28 +1000
    Re: Get classes from "self.MyClass" to improve subclassability Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2015-06-12 12:23 +0000
      Re: Get classes from "self.MyClass" to improve subclassability Thomas Güttler <hv@tbz-pariv.de> - 2015-06-12 05:47 -0700
    Re: Get classes from "self.MyClass" to improve subclassability Terry Reedy <tjreedy@udel.edu> - 2015-06-12 19:08 -0400
      Re: Get classes from "self.MyClass" to improve subclassability Thomas Güttler <hv@tbz-pariv.de> - 2015-06-15 06:14 -0700

#92510 — Get classes from "self.MyClass" to improve subclassability

FromThomas Güttler <hv@tbz-pariv.de>
Date2015-06-12 04:12 -0700
SubjectGet classes from "self.MyClass" to improve subclassability
Message-ID<4d336971-d986-4e21-85a2-ddc8ee995c7e@googlegroups.com>
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? 

[toc] | [next] | [standalone]


#92511

FromChris Angelico <rosuav@gmail.com>
Date2015-06-12 21:28 +1000
Message-ID<mailman.415.1434108514.13271.python-list@python.org>
In reply to#92510
On Fri, Jun 12, 2015 at 9:12 PM, Thomas Güttler <hv@tbz-pariv.de> wrote:
> I prefer "self.Namespace()" to namespace kwargs.
>
> What do you think?

Given that the namespace argument already exists, and you're proposing
a change, you'll need a much stronger justification than mere
preference. What's the downside of providing your own instance, rather
than injecting a class?

ChrisA

[toc] | [prev] | [next] | [standalone]


#92513

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2015-06-12 12:23 +0000
Message-ID<557acf5a$0$11125$c3e8da3@news.astraweb.com>
In reply to#92510
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

[toc] | [prev] | [next] | [standalone]


#92514

FromThomas Güttler <hv@tbz-pariv.de>
Date2015-06-12 05:47 -0700
Message-ID<78f8b0b9-eb7b-40a8-a90d-f2f47b36ad20@googlegroups.com>
In reply to#92513
Hi Steven,

I understand your solution. It is correct and works.

But the missing five characters "self." in the upstream code
produces a lot of more lines in the final result.

Regards,
  Thomas Güttler


Am Freitag, 12. Juni 2015 14:24:06 UTC+2 schrieb Steven D'Aprano:
> 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

[toc] | [prev] | [next] | [standalone]


#92565

FromTerry Reedy <tjreedy@udel.edu>
Date2015-06-12 19:08 -0400
Message-ID<mailman.445.1434150574.13271.python-list@python.org>
In reply to#92510
On 6/12/2015 7:12 AM, 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.

The default arg (None) for the namespace parameter of the 
parse_known_args is an attribute of the function, not of the class or 
instance thereof.  Unless the default Namespace is used elsewhere, this 
seems sensible.

In CPython, at least, and probably in pypy, you can change this 
attribute.  (But AFAIK, this is not guaranteed in all implementations.)

 >>> def f(n = 1): pass

 >>> f.__defaults__
(1,)
 >>> f.__defaults__ = (2,)
 >>> f.__defaults__
(2,)

So the following works

 >>> class C():
	def f(n=1): print(n)
	
 >>> class D(C):
	C.f.__defaults__ = (2,)
	
 >>> D.f()
2

Of course, this may or may not do more than you want.

 >>> C.f()
2

-- 
Terry Jan Reedy

[toc] | [prev] | [next] | [standalone]


#92631

FromThomas Güttler <hv@tbz-pariv.de>
Date2015-06-15 06:14 -0700
Message-ID<6531bd54-d3d5-4b6c-b182-4ac7addc9775@googlegroups.com>
In reply to#92565
Hi,

crazy. I develop python since several years. I was not aware, that you can 
change the defaults of kwargs. I am amazed, but won't use it :-)



Am Samstag, 13. Juni 2015 01:09:47 UTC+2 schrieb Terry Reedy:
> On 6/12/2015 7:12 AM, 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.
> 
> The default arg (None) for the namespace parameter of the 
> parse_known_args is an attribute of the function, not of the class or 
> instance thereof.  Unless the default Namespace is used elsewhere, this 
> seems sensible.
> 
> In CPython, at least, and probably in pypy, you can change this 
> attribute.  (But AFAIK, this is not guaranteed in all implementations.)
> 
>  >>> def f(n = 1): pass
> 
>  >>> f.__defaults__
> (1,)
>  >>> f.__defaults__ = (2,)
>  >>> f.__defaults__
> (2,)
> 
> So the following works
> 
>  >>> class C():
> 	def f(n=1): print(n)
> 	
>  >>> class D(C):
> 	C.f.__defaults__ = (2,)
> 	
>  >>> D.f()
> 2
> 
> Of course, this may or may not do more than you want.
> 
>  >>> C.f()
> 2
> 
> -- 
> Terry Jan Reedy

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web