Path: csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed2a.news.xs4all.nl!xs4all!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.004 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'from:addr:yahoo.co.uk': 0.04; 'subject:Python': 0.06; 'lawrence': 0.09; 'method,': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'def': 0.12; 'jan': 0.12; 'suggest': 0.14; 'language.': 0.14; 'fine.': 0.16; 'fly': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'y):': 0.16; 'language': 0.16; 'fix': 0.17; 'wrote:': 0.18; 'have:': 0.19; '>>>': 0.22; 'import': 0.22; 'header:User-Agent:1': 0.23; 'fraction': 0.24; 'instead.': 0.24; 'class.': 0.26; 'this:': 0.26; 'header:X-Complaints-To:1': 0.27; 'header:In-Reply-To:1': 0.27; 'idea': 0.28; 'chris': 0.29; 'am,': 0.29; "doesn't": 0.30; 'work.': 0.31; '25,': 0.31; 'gary': 0.31; 'idea,': 0.31; "they'll": 0.31; 'class': 0.32; 'brian': 0.33; 'skip:_ 10': 0.34; "i'd": 0.34; 'operations': 0.35; 'but': 0.35; 'add': 0.35; 'false': 0.36; 'method': 0.36; 'should': 0.36; 'skip:- 20': 0.37; 'clear': 0.37; 'being': 0.38; 'to:addr:python- list': 0.38; 'pm,': 0.38; 'that,': 0.38; 'does': 0.39; 'bad': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'how': 0.40; 'ensure': 0.60; 'easy': 0.60; 'most': 0.60; 'new': 0.61; 'our': 0.64; 'more': 0.64; 'charset:windows-1252': 0.65; 'latest': 0.67; 'delegate': 0.68; 'score': 0.74; '2015': 0.84; 'sorry.': 0.91 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Mark Lawrence Subject: Re: Delegation in Python Date: Sat, 24 Jan 2015 23:59:35 +0000 References: <54C42D9D.7010305@digipen.edu> Mime-Version: 1.0 Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: host-92-24-222-48.ppp.as43234.net User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:31.0) Gecko/20100101 Thunderbird/31.4.0 In-Reply-To: <54C42D9D.7010305@digipen.edu> X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 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: 59 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1422144008 news.xs4all.nl 2913 [2001:888:2000:d::a6]:57470 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:84526 On 24/01/2015 23:41, Gary Herron wrote: > On 01/24/2015 03:22 PM, Chris Angelico wrote: >> On Sun, Jan 25, 2015 at 9:57 AM, Brian Gladman wrote: >>> But I am not clear on how to delegate from my new class to the existing >>> Fraction class. This is what I have: >>> >>> -------------------------- >>> class RF(Fraction): >>> >>> def __new__(self, x, y): >>> super().__new__(self, x, y) >>> >>> def is_integer(self): >>> return self.numerator % self.denominator == 0 >>> >>> def __getattr__(self, attr): >>> return getattr(self, attr) >> If you just drop everything but your new method, it should work just >> fine. >> >> class RF(Fraction): >> def is_integer(self): >> return self.numerator % self.denominator == 0 >> >> However, this doesn't ensure that operations on RFs will return more >> RFs - they'll often return Fractions instead. There's no easy fix for >> that, sorry. >> >> ChrisA > > You can always "monkey-path" the Fraction class on the fly to add a new > method to it. I think most would consider this a bad idea, but it does > work. > Try this: > > >>> from fractions import Fraction > >>> def is_integer(self): > ... return self.numerator % self.denominator == 0 > ... > >>> Fraction.is_integer = is_integer # Monkey-patch Fraction > >>> > >>> Fraction(1,2).is_integer() > False > >>> Fraction(2,1).is_integer() > True > > > Gary Herron > As regards this being a bad idea I'd suggest the latest score is Practicality 1 Purity 0 :) -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence