Path: csiph.com!usenet.pasdenom.info!news.albasani.net!news.mixmin.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed6.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.003 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'it;': 0.09; 'terry': 0.09; 'cc:addr:python-list': 0.10; 'def': 0.10; 'subject:not': 0.11; '(self': 0.16; '0.0,': 0.16; 'none"': 0.16; 'reedy': 0.16; 'self.y': 0.16; 'subject:class': 0.16; 'subject:default': 0.16; 'subject:type': 0.16; 'wrote:': 0.17; 'fix': 0.17; 'thanks,': 0.18; 'creates': 0.18; '>>>': 0.18; 'minor': 0.22; 'defined': 0.22; 'cc:2**0': 0.23; 'class.': 0.23; 'needed.': 0.23; 'solutions.': 0.23; 'cc:no real name:2**0': 0.24; 'second': 0.24; 'cc:addr:python.org': 0.25; 'header:In-Reply-To:1': 0.25; 'header :User-Agent:1': 0.26; 'first,': 0.27; 'propose': 0.27; 'skip:_ 10': 0.29; 'class': 0.29; 'function': 0.30; 'point': 0.31; 'pm,': 0.35; 'add': 0.36; 'two': 0.37; 'subject:: ': 0.38; 'instead': 0.39; 'received:192': 0.39; 'received:192.168': 0.40; 'first': 0.61; 'distance': 0.62; 'header:Reply-To:1': 0.68; 'received:74.208': 0.71; 'reply-to:no real name:2**0': 0.72; 'received:74.208.4.194': 0.84 Date: Sat, 10 Nov 2012 17:30:14 -0500 From: Dave Angel User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:16.0) Gecko/20121011 Thunderbird/16.0.1 MIME-Version: 1.0 To: Jennie Subject: Re: Method default argument whose type is the class not yet defined References: <509EBE5A.5080405@gmail.com> In-Reply-To: <509EBE5A.5080405@gmail.com> Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-Provags-ID: V02:K0:DsDRcvaB1PlLJF4Kqi+gIS/n/4S/9IT9Lt61T63fqXm LPC8EB/7RSsgVoyVD34NYmloe8QHCA82fQZre7+aEBLxjMW4Fd FQeD4JtHqQ0ajhUBuM9M5BC/9FW1orlZKLK3GIC9A7Du0XLOgU oUYBqcNUtwbx7n9sTlIZmwQS2e0elWk7GUXXA6rU+gd94wLYON 5mk2SYU5GCYEjKV9vNiKCzn/yLX7e/gidRRpafpxQIiBW9PhDX 432sC4xK19m7FdXw6oFO/XAjvchY4vpPWkRRApUEhjxo9nXw0h ccAzJrH7+AV7o73gPkRcA+Axrbdgaj3IO35r841w/Devlw4aw= = Cc: python-list@python.org X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list Reply-To: d@davea.name 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: 46 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1352586645 news.xs4all.nl 6909 [2001:888:2000:d::a6]:59206 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:33106 On 11/10/2012 03:51 PM, Jennie wrote: > On 11/10/2012 09:29 PM, Terry Reedy wrote: > >> On 11/10/2012 2:33 PM, Jennie wrote: >>> >>> I propose three solutions. The first one: >>> >>> >>> class Point: >>> ... def __init__(self, x=0, y=0): >>> ... self.x = x >>> ... self.y = y >>> ... def __sub__(self, other): >>> ... return Point(self.x - other.x, self.y - other.y) >>> ... def distance(self, point=None): >>> ... p = point if point else Point() >>> ... return math.sqrt((self - p).x ** 2 + (self - p).y ** 2) > >> What I do not like about this one is that it creates a new 0 point each >> time one is needed. Two solutions: >> >> change Point() to point0 in the distance function and create >> point0 = Point() >> after the class. >> >> -or- >> instead of p = line, >> px,py = point.x, point.y if point else 0.0, 0.0 > > Thanks, I like the second one :) > I like the first, once you fix the minor inefficiency in it; add the qualifier "is None" ... def distance(self, point=None): ... p = point if point is None else Point() ... return math.sqrt((self - p).x ** 2 + (self - p).y ** 2) The advantage it then has over the second one is that the whole class is defined inside the class. -- DaveA