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


Groups > comp.lang.python > #92565

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

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!1.eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'defaults': 0.05; 'none:': 0.05; 'subject:skip:s 10': 0.05; 'arg': 0.09; 'attribute.': 0.09; 'namespace': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'snippet': 0.09; 'subclass': 0.09; 'thereof.': 0.09; 'jan': 0.11; 'def': 0.14; '(but': 0.15; '1):': 0.16; 'argparse': 0.16; 'c():': 0.16; 'elsewhere,': 0.16; 'module:': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'reedy': 0.16; 'wrote:': 0.16; 'attribute': 0.18; '>>>': 0.20; 'function,': 0.22; 'parameter': 0.22; 'parser': 0.22; 'pass': 0.22; 'am,': 0.23; 'seems': 0.24; 'header:In-Reply-To:1': 0.24; 'header:User- Agent:1': 0.26; 'header:X-Complaints-To:1': 0.26; 'terry': 0.29; 'class.': 0.31; 'probably': 0.32; 'class': 0.33; 'could': 0.35; 'to:addr:python-list': 0.35; 'instance': 0.35; 'subject:" ': 0.36; 'subject:: ': 0.37; 'received:org': 0.38; 'skip:p 20': 0.38; 'to:addr:python.org': 0.39; 'easy': 0.60; 'default': 0.61; 'more': 0.62; 'thomas': 0.63; 'skip:n 10': 0.63; 'different': 0.64; 'here': 0.66; 'subject:Get': 0.66; 'guaranteed': 0.67; 'subject:improve': 0.84; 'received:fios.verizon.net': 0.91
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Terry Reedy <tjreedy@udel.edu>
Subject Re: Get classes from "self.MyClass" to improve subclassability
Date Fri, 12 Jun 2015 19:08:43 -0400
References <4d336971-d986-4e21-85a2-ddc8ee995c7e@googlegroups.com>
Mime-Version 1.0
Content-Type text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding quoted-printable
X-Gmane-NNTP-Posting-Host pool-98-114-97-173.phlapa.fios.verizon.net
User-Agent Mozilla/5.0 (Windows NT 6.1; WOW64; rv:31.0) Gecko/20100101 Thunderbird/31.7.0
In-Reply-To <4d336971-d986-4e21-85a2-ddc8ee995c7e@googlegroups.com>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.20+
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.445.1434150574.13271.python-list@python.org> (permalink)
Lines 58
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1434150574 news.xs4all.nl 2948 [2001:888:2000:d::a6]:45060
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:92565

Show key headers only | View raw


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 | Next 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