【问题标题】:Implementing a point class in Python在 Python 中实现点类
【发布时间】:2016-03-03 21:23:07
【问题描述】:

所以我正在尝试实现一个点类,它创建一个点,然后旋转、缩放和平移该点。这是我目前写的。

class Point:
    '''
        Create a Point instance from x and y.
    '''
    def __init__(self, x, y):
        self.x = 0
        self.y = 0

    '''
        Rotate counterclockwise, by a radians, about the origin.
    '''
    def rotate(self, a):
        self.x0 = math.cos(this.a) * self.x - math.sin(this.a) * self.y
        self.y0 = math.sin(this.a) * self.x + math.cos(this.a) * self.y

    '''

        Scale point by factor f, about the origin.
    Exceptions
        Raise Error if f is not of type float.
    '''
    def scale(self, f):
        self.x0 = f * self.x
        self.y0 = f * self.y

    '''
        Translate point by delta_x and delta_y.
    Exceptions
        Raise Error if delta_x, delta_y are not of type float.
    '''
    def translate(self, delta_x, delta_y):
        self.x0 = self.x + delta_x
        self.y0 = self.y + delta_y

    '''
        Round and convert to int in string form.
    '''
    def __str__(self):
        return int(round(self.x))

此代码中的某些内容正在生成错误。现在我还没有实现错误捕获,我在顶部确实有一个错误方法

class Error(Exception):
    def __init__(self, message):
        self.message = message

但是,如果某个变量不是浮点类型,我将如何捕捉错误?

这是我正在使用的 if 语句之一:

def __init__(self, x, y):
        if not isinstance(x, float):
            raise Error ("Parameter \"x\" illegal.")        
            self.x = x
            self.y = y
        if not isinstance(y, float):
            raise Error ("Parameter \"y\" illegal.")
            self.x = x
            self.y = y

但这会给我一个缩进错误。那么我如何才能准确地打印出一条错误消息,准确说明是哪个变量导致了问题呢?

【问题讨论】:

  • “此代码中的某些内容正在生成错误。”是什么产生了错误? (提示:错误信息告诉你。)
  • 不应该 self.x = 0self.x = xself.y = 0self.y = y
  • 错误显示“ Traceback (最近一次调用最后一次): File "test_A.py", line 17, in print Point(0.0,1.0) TypeError: str 返回非字符串(int 类型)"
  • 为什么要关心变量是float?如果你想确保它是一个数字,你可以查询变量是否是数字类型的实例,如果不是自己抛出自定义错误......
  • 您的错误是因为您将 str 的返回值转换为 int 而不是 str

标签: python error-handling point


【解决方案1】:

如果要引发异常,请在 Point 的初始化程序中执行:

def __init__(self, x, y):
    if not isinstance(x, float) or not isinstance(y, float):
        raise Error("Point's coordinates must be floats.")
    self.x = x
    self.y = y

或者将坐标转换为浮点数:

def __init__(self, x, y):
    self.x = float(x)
    self.y = float(y)

【讨论】:

  • 我在这里使用了错误类,因为你提到你已经定义了它,但它可能应该更具体的异常
  • 应该 self.x = x 和 self.y = y 在缩进内,因为我一直收到该行的错误。
  • 好的,它似乎可以捕获错误,但是如果我希望错误消息显示参数“x”非法或参数“a”非法,并实际命名导致错误的变量。
  • @DavidRolfe 在这种情况下,当我们只讨论 2 个变量 x 和 y 时,只需让 if 语句更详细。
  • 我尝试使用两个 if 语句,它一直说 IndentationError: unindent does not match any external indentation level
【解决方案2】:

如果变量不是浮点数,你会得到一个 TypeError。你几乎可以像这样“捕捉”这些错误;

try:
    pass # your stuff here.
except e as TypeError:
    print e # this means a type error has occurred, type is not correct.

另外,这对于在断言开头检查正确的类型是值得一读的; https://wiki.python.org/moin/UsingAssertionsEffectively

【讨论】:

    猜你喜欢
    • 2021-06-09
    • 1970-01-01
    • 2022-07-07
    • 2010-10-07
    • 2011-08-23
    • 1970-01-01
    相关资源
    最近更新 更多