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


Groups > comp.lang.python > #33097

Method default argument whose type is the class not yet defined

From Jennie <nameDOTportua@gmail.com>
Newsgroups comp.lang.python
Subject Method default argument whose type is the class not yet defined
Date 2012-11-10 20:33 +0100
Organization Aioe.org NNTP Server
Message-ID <k7ma5c$qdk$1@speranza.aioe.org> (permalink)

Show all headers | View raw


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 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

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

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?
-- 
Jennie

Back to comp.lang.python | Previous | NextNext 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