Making a Point Class in Python

霸气de小男生 提交于 2019-11-29 05:21:11

Don't forget math.hypot

def distance(self, p):
    dx = self.X - p.X
    dy = self.Y - p.Y
    return hypot(dx, dy)
  • You declared distance as taking an argument p; inside the method you're referring to it as other. Change p to other in the declaration so they match.

  • sqrt() isn't a builtin; you need to do import math and refer to it as math.sqrt().

  • You aren't doing anything with the testPoint() function you declare; you can invoke it by adding a line at the end like:

print "distance = %s"%(testPoint())

At that point, your code works and computes a distance of 4.0 between your points.

Now, some style issues:

  • In Python, you don't generally privatize member variables, and you don't bother writing trivial getters and setters, so you can remove the getX() and getY() methods and just refer to p.X and p.Y directly given a Point p.

  • The math module has a convenient hypotenuse function, so in distance() you can change the return line to return math.hypot(dx,dy).

  • By default, a user defined object has an unattractive string representation:

    <__main__.Point object at 0x1004e4550>

You should define a string conversion method in your class like so:

    def __str__(self):
        return "Point(%s,%s)"%(self.X,self.Y)

This will be used when the object is printed, or otherwise needs to be converted to a string.

In your Point.distance method, you reference other.X and other.Y; Other does not exists.

You should either change the distance signature to be distance(self, other) or change the code to use p.

You will also need to import math.sqrt:

from math import sqrt

Hum, why not use complex instead of a point class? It has all the properties you are searching for and more (such as rotation).

Here is an example to "OOP" the complex with a pedantic notation:

https://gist.github.com/jul/9286835

AndrewJesaitis

Since you asked about executing the program, if you want to run it from the command line, the first thing you should do is just add:

if __name__ == '__main__':
    testPoint()

There are really good explanations of what this does (see: What does if __name__ == "__main__": do?). But at the highest level, this will call your test function when you enter python yourfilename.py at the command prompt. The you can start debugging by seeing what errors are thrown and what happens when you make the changes given in the other answers.

To incement COUNT everytime you create a point to your __init__ you'd add: Point.COUNT += 1. COUNT is an class variable of the Point class. A quick note on style, PEP 8 says that "constants are usually defined on a module level and written in all capital letters with underscores separating words." So you'd want to call it count since it is a mutable class variable. Finally at the bottom of your testPoint function add a line print Point.count so you can see how many points you created.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!