I suggest you consider creating a Point class using the collections.namedtuple factory function, which will make it a subclass of the built-in class, in the tuple class. This will save you some work on the slabs. The namedtuple class has attributes that can be accessed by name, for example px , and by index, for example p[0] .
They are also very memory efficient, such as tuple s, which can be important if you have many instances of the class.
You can also specialize in what is returned by subclassing, or use the verbose parameter to capture the source code and, if necessary, change this parameter.
Here is an example in the above documentation showing that it is used to create a 2D Point class, which seems to be very useful in your particular use case.
Here is an example showing how you can define your own 3D Point class through a subclass:
from collections import namedtuple class Point(namedtuple('Point', 'xy z')): __slots__ = ()
Conclusion:
p1: Point(x=1, y=2, z=3) p2: Point(21, 22, 23) distance p1 <-> p2: 34.641
Note that when using namedtuple you do not need to implement __getitem__() yourself and not write the __str__() method. The only reason __init__() was needed was because of the need to increase the counter of instances of the class that was added, because namedtuple does not have or does by default.
source share