Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I suggest you consider making your Point class using the <a href="http://docs.python.org/library/collections.html?highlight=namedtuple#collections.namedtuple" rel="nofollow">collections.namedtuple</a> factory function which will make it a subclass of the the built-in <code>tuple</code> class. This will save you some boiler-plate work. <code>namedtuple</code> class have attributes that can be accessed both by name, such as <code>p.x</code> and indexed, like <code>p[0]</code>. </p> <p>They are also very memory efficient like <code>tuple</code>s, which may be important if you're going to have a lot of class instances.</p> <p>You can further specialize what is returned by subclassing it, or use the <code>verbose</code> option to capture the source code and modify that as necessary.</p> <p>There's an example in the documentation linked to above showing it being used to create a 2D <code>Point</code> class, which seems like it could be very helpful in your specific use-case.</p> <p>Here's an example showing how one could define a custom 3D Point class via subclassing:</p> <pre><code>from collections import namedtuple class Point(namedtuple('Point', 'x y z')): __slots__ = () # prevent creation of instance dictionaries to save memory point_count = 0 # instance counter def __init__(self, *args): super(Point, self).__init__(*args) Point.point_count += 1 def distance(self, other): return sum((self[i]-other[i])**2 for i in xrange(len(self))) ** 0.5 def copy_point(self, distance): 'create another Point at distance from the self Point' return Point(*[dimension+distance for dimension in self]) p1 = Point(0, 0, 0) print 'p1:', p1 p2 = p1.copy_point(20) print 'p2: Point(%s)' % ', '.join(str(p2[i]) for i in xrange(len(p2))) print 'distance p1 &lt;-&gt; p2: %.3f' % p1.distance(p2) </code></pre> <p>Output:</p> <pre class="lang-none prettyprint-override"><code>p1: Point(x=1, y=2, z=3) p2: Point(21, 22, 23) distance p1 &lt;-&gt; p2: 34.641 </code></pre> <p>Note that by using <code>namedtuple</code> you don't have to implement a <code>__getitem__()</code> yourself, nor write a <code>__str__()</code> method. The only reason an <code>__init__()</code> was needed was because of the need to increment the class instance counter which was added -- something that <code>namedtuple</code>s don't have or do by default.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload