Path: csiph.com!usenet.pasdenom.info!aioe.org!.POSTED!not-for-mail From: Jennie Newsgroups: comp.lang.python Subject: Re: Method default argument whose type is the class not yet defined Date: Sat, 10 Nov 2012 21:51:38 +0100 Organization: Aioe.org NNTP Server Lines: 31 Message-ID: <509EBE5A.5080405@gmail.com> References: NNTP-Posting-Host: wGiY0AegsjthpzXyo2QWBw.user.speranza.aioe.org Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Complaints-To: abuse@aioe.org User-Agent: Mozilla/5.0 (X11; Linux i686; rv:16.0) Gecko/20121011 Thunderbird/16.0.1 X-Notice: Filtered by postfilter v. 0.8.2 Xref: csiph.com comp.lang.python:33101 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 :) -- Jennie