Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed5.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.005 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'instantiated': 0.09; 'tuple': 0.09; 'def': 0.10; 'subject:not': 0.11; '(self': 0.16; 'args:': 0.16; 'fiddle': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'self.y': 0.16; 'subject:class': 0.16; 'subject:default': 0.16; 'subject:type': 0.16; 'wrote:': 0.17; '>>>': 0.18; 'skip:p 30': 0.20; "i'd": 0.22; 'header:In- Reply-To:1': 0.25; 'am,': 0.27; 'message-id:@mail.gmail.com': 0.27; 'skip:_ 10': 0.29; 'class': 0.29; 'point': 0.31; 'could': 0.32; '11,': 0.33; 'to:addr:python-list': 0.33; 'received:google.com': 0.34; 'third': 0.34; 'nov': 0.35; 'received:209.85': 0.35; 'one,': 0.37; 'received:209': 0.37; 'subject:: ': 0.38; 'to:addr:python.org': 0.39; 'header:Received:5': 0.40; 'distance': 0.62; 'ever': 0.63; 'confusing': 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 :content-type; bh=9n/Z8mL+QS8p9AVXDbrZx2jhhSPpOTawkwmE+nu8T78=; b=Ut6PhXOn66L2ThCGISiQMa9KOSEOKPkSEOr6WueRGMVwKsZjlvYnwRX/2Nor2WH4Tv 21d0rz+IoxfT2scNmJRJFQYzQaQUO6pGg/sxGeUklhbb8DtvYwn27EPKW7+8YwDXox8k FGPFr50QztmkczvRqI5QP+XSIHtpVxE4bdduL5DKMn+sRZYMzcjWXva4uKorrEEkFEMk KCjSmecySMpoWmPFo7yf/Z/5gIwVnP0/Fea0D61YyzbWI6yUYsO9wZUfEJlz60UX8Mmz 4CAjI+f41VmP3oAO5fDVHYcD0NuosT0OSbrTWy/uYWyfd6bVICCOiPWCtpwc0o2hIEuf 5kgg== MIME-Version: 1.0 In-Reply-To: References: Date: Sun, 11 Nov 2012 06:56:17 +1100 Subject: Re: Method default argument whose type is the class not yet defined From: Chris Angelico To: python-list@python.org Content-Type: text/plain; charset=ISO-8859-1 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: 21 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1352577386 news.xs4all.nl 6923 [2001:888:2000:d::a6]:57329 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:33098 On Sun, Nov 11, 2012 at 6:33 AM, Jennie wrote: > ... def distance(self, point=None): > ... p = point if point else Point() I'd go with this one. Definitely not the third one, which mutates the class according to a current global every time a Point is instantiated - could be *extremely* confusing if the name distance were ever rebound. You could also fiddle with the default args: >>> 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 - p).x ** 2 + (self - p).y ** 2) >>> Point.distance.__defaults__=Point(), # has to be a tuple ChrisA