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


Groups > comp.lang.python > #33106

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

Path csiph.com!usenet.pasdenom.info!news.albasani.net!news.mixmin.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 <d@davea.name>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.003
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'it;': 0.09; 'terry': 0.09; 'cc:addr:python-list': 0.10; 'def': 0.10; 'subject:not': 0.11; '(self': 0.16; '0.0,': 0.16; 'none"': 0.16; 'reedy': 0.16; 'self.y': 0.16; 'subject:class': 0.16; 'subject:default': 0.16; 'subject:type': 0.16; 'wrote:': 0.17; 'fix': 0.17; 'thanks,': 0.18; 'creates': 0.18; '>>>': 0.18; 'minor': 0.22; 'defined': 0.22; 'cc:2**0': 0.23; 'class.': 0.23; 'needed.': 0.23; 'solutions.': 0.23; 'cc:no real name:2**0': 0.24; 'second': 0.24; 'cc:addr:python.org': 0.25; 'header:In-Reply-To:1': 0.25; 'header :User-Agent:1': 0.26; 'first,': 0.27; 'propose': 0.27; 'skip:_ 10': 0.29; 'class': 0.29; 'function': 0.30; 'point': 0.31; 'pm,': 0.35; 'add': 0.36; 'two': 0.37; 'subject:: ': 0.38; 'instead': 0.39; 'received:192': 0.39; 'received:192.168': 0.40; 'first': 0.61; 'distance': 0.62; 'header:Reply-To:1': 0.68; 'received:74.208': 0.71; 'reply-to:no real name:2**0': 0.72; 'received:74.208.4.194': 0.84
Date Sat, 10 Nov 2012 17:30:14 -0500
From Dave Angel <d@davea.name>
User-Agent Mozilla/5.0 (X11; Linux x86_64; rv:16.0) Gecko/20121011 Thunderbird/16.0.1
MIME-Version 1.0
To Jennie <marco.buttu@gmail.com>
Subject Re: Method default argument whose type is the class not yet defined
References <k7ma5c$qdk$1@speranza.aioe.org> <mailman.3541.1352579400.27098.python-list@python.org> <509EBE5A.5080405@gmail.com>
In-Reply-To <509EBE5A.5080405@gmail.com>
Content-Type text/plain; charset=ISO-8859-1
Content-Transfer-Encoding 7bit
X-Provags-ID V02:K0:DsDRcvaB1PlLJF4Kqi+gIS/n/4S/9IT9Lt61T63fqXm LPC8EB/7RSsgVoyVD34NYmloe8QHCA82fQZre7+aEBLxjMW4Fd FQeD4JtHqQ0ajhUBuM9M5BC/9FW1orlZKLK3GIC9A7Du0XLOgU oUYBqcNUtwbx7n9sTlIZmwQS2e0elWk7GUXXA6rU+gd94wLYON 5mk2SYU5GCYEjKV9vNiKCzn/yLX7e/gidRRpafpxQIiBW9PhDX 432sC4xK19m7FdXw6oFO/XAjvchY4vpPWkRRApUEhjxo9nXw0h ccAzJrH7+AV7o73gPkRcA+Axrbdgaj3IO35r841w/Devlw4aw= =
Cc python-list@python.org
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
Reply-To d@davea.name
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.3545.1352586645.27098.python-list@python.org> (permalink)
Lines 46
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1352586645 news.xs4all.nl 6909 [2001:888:2000:d::a6]:59206
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:33106

Show key headers only | View raw


On 11/10/2012 03:51 PM, Jennie wrote:
> 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 :)
>
I like the first, once you fix the minor inefficiency in it;  add the
qualifier "is None"


...     def distance(self, point=None):
...         p = point if point is None else Point()
...         return math.sqrt((self - p).x ** 2 + (self - p).y ** 2)

The advantage it then has over the second one is that the whole class is
defined inside the class.

-- 

DaveA

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