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: 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 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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: 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 On 6/12/2015 7:12 AM, Thomas G=C3=BCttler wrote: > Here is a snippet from the argparse module: > > {{{ > def parse_known_args(self, args=3DNone, namespace=3DNone): > ... > # default Namespace built from parser defaults > if namespace is None: > namespace =3D Namespace() # < =3D=3D=3D=3D=3D=3D=3D my iss= ue > }}} > > 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 =3D 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=20 parse_known_args is an attribute of the function, not of the class or=20 instance thereof. Unless the default Namespace is used elsewhere, this=20 seems sensible. In CPython, at least, and probably in pypy, you can change this=20 attribute. (But AFAIK, this is not guaranteed in all implementations.) >>> def f(n =3D 1): pass >>> f.__defaults__ (1,) >>> f.__defaults__ =3D (2,) >>> f.__defaults__ (2,) So the following works >>> class C(): def f(n=3D1): print(n) =09 >>> class D(C): C.f.__defaults__ =3D (2,) =09 >>> D.f() 2 Of course, this may or may not do more than you want. >>> C.f() 2 --=20 Terry Jan Reedy