Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #33111

Re: Method default argument whose type is the class not yet defined

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 <oscar.j.benjamin@gmail.com>
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 <k7ma5c$qdk$1@speranza.aioe.org>
References <k7ma5c$qdk$1@speranza.aioe.org>
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 <oscar.j.benjamin@gmail.com>
To Jennie <nameDOTportua@gmail.com>
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 <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.3547.1352596990.27098.python-list@python.org> (permalink)
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

Show key headers only | View raw


On 10 November 2012 19:33, Jennie <nameDOTportua@gmail.com> 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 "<stdin>", line 1, in <module>
>   File "<stdin>", 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

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Method default argument whose type is the class not yet defined Jennie <nameDOTportua@gmail.com> - 2012-11-10 20:33 +0100
  Re: Method default argument whose type is the class not yet defined Chris Angelico <rosuav@gmail.com> - 2012-11-11 06:56 +1100
  Re: Method default argument whose type is the class not yet defined Terry Reedy <tjreedy@udel.edu> - 2012-11-10 15:29 -0500
    Re: Method default argument whose type is the class not yet defined Jennie <nameDOTportua@gmail.com> - 2012-11-10 21:51 +0100
      Re: Method default argument whose type is the class not yet defined Dave Angel <d@davea.name> - 2012-11-10 17:30 -0500
    Re: Method default argument whose type is the class not yet defined Jennie <marco.buttu@gmail.com> - 2012-11-10 21:51 +0100
  Re: Method default argument whose type is the class not yet defined Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-11-11 01:13 +0000
    Re: Method default argument whose type is the class not yet defined Chris Angelico <rosuav@gmail.com> - 2012-11-11 13:13 +1100
    Re: Method default argument whose type is the class not yet defined Ian Kelly <ian.g.kelly@gmail.com> - 2012-11-10 19:43 -0700
      Re: Method default argument whose type is the class not yet defined Roy Smith <roy@panix.com> - 2012-11-10 21:53 -0500
        Re: Method default argument whose type is the class not yet defined Ian Kelly <ian.g.kelly@gmail.com> - 2012-11-10 23:43 -0700
        Re: Method default argument whose type is the class not yet defined Ian Kelly <ian.g.kelly@gmail.com> - 2012-11-10 23:45 -0700
    Re: Method default argument whose type is the class not yet defined Chris Angelico <rosuav@gmail.com> - 2012-11-11 13:47 +1100
    Re: Method default argument whose type is the class not yet defined Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2012-11-11 14:21 +0000
      Re: Method default argument whose type is the class not yet defined Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-11-11 22:31 +0000
        Re: Method default argument whose type is the class not yet defined Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2012-11-12 00:31 +0000
          Re: Method default argument whose type is the class not yet defined Steve Howell <showell30@yahoo.com> - 2012-11-11 16:56 -0800
          Re: Method default argument whose type is the class not yet defined Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-11-12 04:46 +0000
        Re: Method default argument whose type is the class not yet defined Mark Lawrence <breamoreboy@yahoo.co.uk> - 2012-11-12 01:10 +0000
          Re: Method default argument whose type is the class not yet defined Roy Smith <roy@panix.com> - 2012-11-11 20:15 -0500
            Re: Method default argument whose type is the class not yet defined Mark Lawrence <breamoreboy@yahoo.co.uk> - 2012-11-12 01:35 +0000
        Re: Method default argument whose type is the class not yet defined Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2012-11-12 01:18 +0000
          Re: Method default argument whose type is the class not yet defined Roy Smith <roy@panix.com> - 2012-11-11 20:34 -0500
        Re: Method default argument whose type is the class not yet defined Mark Lawrence <breamoreboy@yahoo.co.uk> - 2012-11-12 01:29 +0000
        Re: Method default argument whose type is the class not yet defined Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2012-11-12 01:50 +0000
  Re: Method default argument whose type is the class not yet defined Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2012-11-11 01:23 +0000
  Re: Method default argument whose type is the class not yet defined Steve Howell <showell30@yahoo.com> - 2012-11-11 16:32 -0800

csiph-web