Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'python': 0.09; '"""return': 0.09; 'nameerror:': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'terry': 0.09; 'def': 0.10; 'subject:not': 0.11; '(self': 0.16; '0.0,': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'reedy': 0.16; 'self.y': 0.16; 'subject:class': 0.16; 'subject:default': 0.16; 'subject:type': 0.16; 'wrote:': 0.17; 'creates': 0.18; 'jan': 0.18; '>>>': 0.18; 'math': 0.20; 'import': 0.21; '"",': 0.22; 'defined': 0.22; 'class.': 0.23; 'needed.': 0.23; 'solutions.': 0.23; 'second': 0.24; 'header:In-Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; '(most': 0.27; 'propose': 0.27; 'header:X-Complaints-To:1': 0.28; 'skip:_ 10': 0.29; 'class': 0.29; 'function': 0.30; 'point': 0.31; 'file': 0.32; 'traceback': 0.33; 'problem': 0.33; 'to:addr:python- list': 0.33; 'pm,': 0.35; 'there': 0.35; 'received:org': 0.36; 'two': 0.37; 'subject:: ': 0.38; 'instead': 0.39; 'to:addr:python.org': 0.39; 'header:Received:5': 0.40; 'first': 0.61; 'distance': 0.62; 'solve': 0.62; 'received:fios.verizon.net': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Terry Reedy Subject: Re: Method default argument whose type is the class not yet defined Date: Sat, 10 Nov 2012 15:29:39 -0500 References: Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: pool-173-75-251-66.phlapa.fios.verizon.net User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:15.0) Gecko/20120824 Thunderbird/15.0 In-Reply-To: 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: 92 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1352579400 news.xs4all.nl 6908 [2001:888:2000:d::a6]:38928 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:33100 On 11/10/2012 2:33 PM, Jennie wrote: > What is the best solution to solve the following problem in Python 3.3? > > import math > >>> 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=Point()): > ... """Return the distance from `point`.""" > ... return math.sqrt((self - point).x ** 2 + (self - point).y ** 2) > ... > Traceback (most recent call last): > File "", line 1, in > File "", line 5, in Point > NameError: name 'Point' is not defined > > 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) > ... > >>> p = Point() > >>> p.distance() > 0.0 > >>> p.distance(Point(3, 4)) > 5.0 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 > The second 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=Point()): > ... return math.sqrt((self - point).x ** 2 + (self - point).y ** 2) > ... > >>> Point.distance = distance > >>> p = Point() > >>> p.distance(Point(3, 4)) > 5.0 my first thought > The last one: > > >>> class Point: > ... def __init__(self, x=0, y=0): > ... self.x = x > ... self.y = y > ... Point.distance = distance > ... def __sub__(self, other): > ... return Point(self.x - other.x, self.y - other.y) > ... > >>> def distance(self, point=Point()): > ... return math.sqrt((self - point).x ** 2 + (self - point).y ** 2) > ... > >>> p = Point() > >>> p.distance(Point(3, 4)) > 5.0 > > Is there a better solution? -- Terry Jan Reedy