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


Groups > comp.lang.python > #92631

Re: Get classes from "self.MyClass" to improve subclassability

Newsgroups comp.lang.python
Date 2015-06-15 06:14 -0700
References <4d336971-d986-4e21-85a2-ddc8ee995c7e@googlegroups.com> <mailman.445.1434150574.13271.python-list@python.org>
Message-ID <6531bd54-d3d5-4b6c-b182-4ac7addc9775@googlegroups.com> (permalink)
Subject Re: Get classes from "self.MyClass" to improve subclassability
From Thomas Güttler <hv@tbz-pariv.de>

Show all headers | View raw


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

Back to comp.lang.python | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

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

csiph-web