Python程序在遇到错误后继续运行

时间:2015-12-25 06:17:59

标签: class python-3.x linear-algebra theory

我正在使用向量和矩阵类为我的线性代数类创建程序,但是我在编写矩阵类的字符串时遇到了麻烦。造成问题的原因是if语句在矩阵中的条目之后添加逗号,如果它不是行中的最后一个条目。对此我感到好奇的是我已经将问题分离到我的程序部分,该部分将变量分配给手头的条目的索引,但是当我在那之后添加一行来打印该变量以尝试并弄清楚发生了什么,运行程序打印变量然后给出了之前的行错误。这是代码:

import copy
class vector:

    def __init__(self, entries):
        if type(entries) == list:

            self.elements = []
            self.dimensionality = len(entries)
            for entry in entries:
                self.elements.append(entry)
        if type(entries) == vector:
            self.elements = entries.elements

    def __str__(self):
        buff = "("
        for e in self.elements:
            buff += str(e)
            if self.elements.index(e) < len(self.elements) - 1:
                buff += ", "
        buff += ")"
        return buff
    def __getitem__(self,index):
        return self.elements[index]

    def __len__(self):
        return len(self.elements)

    def __mul__(self, otherVector):
        if self.dimensionality != otherVector.dimensionality:
            raise RuntimeError("Cannot multiply vectors of different dimensions")
        else:
            product = 0
            for e in self.elements:
                product += e * otherVector.elements[self.elements.index(e)]
            return product

    def __eq__(self, otherVariable):
        return size(self) == size(otherVariable)

    def size(x):
        return (x * x)**(1/2)



class matrix:
    def __init__(self, entries):
        for i in entries:
            if len(entries[0]) != len(i):
                raise RuntimeError("All rows of matrix must contain the same number of entries")
        self.elements = []
        for row in entries:
            self.elements.append(vector(row))
    def __str__(self):
        buff = "("
        for row in self.elements:
            buff += str(row)
            a = self.elements.index(row)   #this is the line that prompts the error
            b = len(self.elements) - 1
            print (a)                     #but this line executes before the error cuts off the rest of the program
            print(b)
            print(a<b)
            if a < b :
                buff += ", "
        buff += ")"
        return buff

print(matrix([[1,2],[2,3]]))

以及它给我的错误:

Traceback (most recent call last):
  File "/Users/sebpole/Documents/vectors.py", line 127, in <module>
    print(matrix([[1,2],[2,3]]))
  File "/Users/sebpole/Documents/vectors.py", line 83, in __str__
    a = self.elements.index(row)
  File "/Users/sebpole/Documents/vectors.py", line 38, in __eq__
    return size(self) == size(otherVariable)
NameError: name 'size' is not defined

我通过跳过函数定义&#39; size()&#39;来修正了这个特定的错误。并将其写入向量相等的定义。由于它很短并且没有问题,并且在调整之后程序运行正常,但我有以下两个概念性问题: 1)在错误发生之前执行错误后,该行正在进行什么? 2)究竟是什么问题?为什么程序在调用稍后定义的函数时遇到问题?为什么取一行矩阵的索引会调用该行的等式定义(向量)?

2 个答案:

答案 0 :(得分:0)

使用[ [ {'latitude': 40.7136948694776, 'days': "['MON']", 'from_time': '11AM', 'longtitude': -73.9869100091687, 'to_time': '12:30PM', 'signdesc1': 'NO PARKING (SANITATION BROOM SYMBOL) 11AM TO 12:30PM MON & THURS'}, {'latitude': 40.7136948694776, 'days': "['MON']", 'from_time': '11AM', 'longtitude': -73.9869100091687, 'to_time': '12:30PM', 'signdesc1': 'NO PARKING (SANITATION BROOM SYMBOL) 11AM TO 12:30PM MON & FRI'} ], [ {'latitude': 40.7136948694776, 'days': "['MON']", 'from_time': '11AM', 'longtitude': -73.9869100091688, 'to_time': '12:30PM', 'signdesc1': 'NO PARKING (SANITATION BROOM SYMBOL) 11AM TO 12:30PM MON & WED'} ] ] 来引用课程中定义的功能

答案 1 :(得分:0)

错误来自第127行中对print的调用,并且未执行整行。你真的在控制台中看到打印输出,而不是堆栈跟踪吗?

从概念上讲,相关的行print(matrix([[1,2],[2,3]]))执行此操作:

  • matrix实例已成功创建
  • print__str__实例
  • 上调用matrix
  • __str__index个实例
  • 列表中调用vector
  • index需要查看列表以查找匹配值,并在列表的每个成员上调用__eq__以查找匹配项
  • 您的原始__eq__代码调用名为size的丢失函数(您注意到并修复了该函数)

我很惊讶这产生了除错误之外的任何输出。