Path: csiph.com!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!newsfeed.eweka.nl!eweka.nl!feeder3.eweka.nl!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.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'python': 0.09; '"""return': 0.09; 'collections': 0.09; 'nameerror:': 0.09; 'tuple': 0.09; 'cc:addr:python-list': 0.10; 'def': 0.10; 'subject:not': 0.11; '(self': 0.16; 'instance:': 0.16; 'namedtuple': 0.16; 'self.y': 0.16; 'subject:class': 0.16; 'subject:default': 0.16; 'subject:type': 0.16; 'wrote:': 0.17; '>>>': 0.18; 'math': 0.20; 'import': 0.21; '"",': 0.22; 'defined': 0.22; 'cc:2**0': 0.23; 'cc:no real name:2**0': 0.24; 'cc:addr:python.org': 0.25; 'header:In-Reply-To:1': 0.25; '(most': 0.27; 'message-id:@mail.gmail.com': 0.27; '>>>>': 0.29; 'skip:_ 10': 0.29; 'class': 0.29; 'received:209.85.215.46': 0.30; 'point': 0.31; 'file': 0.32; 'could': 0.32; 'traceback': 0.33; 'problem': 0.33; 'received:google.com': 0.34; 'received:209.85': 0.35; 'skip:p 20': 0.36; 'received:209': 0.37; 'subject:: ': 0.38; 'header:Received:5': 0.40; 'distance': 0.62; 'solve': 0.62; 'oscar': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; bh=c/mP0MHmorfQWeE6OJbefM9l2rq4BbJItTORe5L5tCI=; b=DP/GKSu9BRCD88hvctE9wQLhi239TNBYgdUraZfPFiIFXo7a/7pfOqGTel5MUw1qNq VtpvsTjH0aZan4uT+znNs61z4bzXMF8gAXmYUlFZPXS0QuRsD6qxqexpyKZs281HS/Tu rCR4CvZfRE8y3rmCJ26uROF1tf3D+5cdW2vBHo+0gIxR06UsK3ypbNSBwUVJ9db4DwJU iipdX1KcRK63K/0hXLuGdy6gk44IH154AnpI31ZWHAdg/GDugpCIZfSbGdooWimLBbcX L1+IMfaFKAF7BJT4BTuQ0RjgQTECEy/+DSrkoX1PHYNqAmhX8FRxR/zc/dUfT0KHkFEB hfuA== MIME-Version: 1.0 In-Reply-To: References: Date: Sun, 11 Nov 2012 01:23:08 +0000 Subject: Re: Method default argument whose type is the class not yet defined From: Oscar Benjamin To: Jennie Content-Type: text/plain; charset=ISO-8859-1 Cc: python-list@python.org 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: 38 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1352596990 news.xs4all.nl 6983 [2001:888:2000:d::a6]:53987 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:33111 On 10 November 2012 19:33, 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 would use namedtuple and make it so that an ordinary tuple could be used as in place of a Point instance: >>> import math >>> from collections import namedtuple >>> class Point(namedtuple('Point', ['x', 'y'])): ... def distance(self, other=(0, 0)): ... (myx, myy), (theirx, theiry) = self, other ... return math.sqrt((myx - theirx) ** 2 + (myy - theiry) ** 2) ... >>> p = Point(3, 4) >>> p.distance() 5.0 >>> p2 = Point(4, 5) >>> p.distance(p2) 1.4142135623730951 Oscar