[英]In an instance of a class, can you use a value returned from a one method, as a parameter for calling another method
class A:
def __init__(self, matrix=[]):
self.matrix = matrix
def dimension(self):
return len(self.matrix), len(self.matrix[0])
def reduce_matrix(self, i, j):
temp = self.matrix[:i-1] + self.matrix[i:]
M = A([row[:j-1] + row[j:] for row in temp])
return M
def determinant(self):
(nrows, ncols) = self.dimension()
if nrows != ncols:
return ("Cannot find determinant of non-square matrix.")
elif nrows == 1:
return self.matrix[0][0]
else:
M = A(sum([ ((-1)**j) * self.matrix[0][j] * self.reduce_matrix(1,
j+1).determinant() for j in range(ncols) ]))
return M
Hello, I just started OOP so I have a question regarding using the returned value of a method in place of the "instance name" that is passed as "self" for some other method.你好,我刚刚开始 OOP,所以我有一个问题,关于使用方法的返回值代替作为“self”传递给其他方法的“实例名称”。
I included the rest of the code that just makes the calculations for a matrix problem, but I'm only focusing on the " self.reduce_matrix(1, j+1)).determinant()" bit of it.我包含了仅用于计算矩阵问题的其余代码,但我只关注其中的“ self.reduce_matrix(1, j+1)).determinant()”位。 I need to take a matrix (which is a list of lists ex. [[1,2,3],[4,5,6]] ), and perform the "reduce_matrix" method 1 time per column in the matrix, and each time, the "determinant(self)" method passes in the value returned from the other method.
我需要获取一个矩阵(这是一个列表列表,例如 [[1,2,3],[4,5,6]] ),并在矩阵中的每列执行“reduce_matrix”方法 1 次,并且每次,“determinant(self)”方法都会传入另一个方法返回的值。 When I run it, it says that "list object has no attribute "determinant"" because I can't pass in "self" to the determinant method like I usually do --> "self.determinant(self)"
当我运行它时,它说“列表对象没有属性“决定因素””,因为我不能像往常一样将“自我”传递给决定因素方法 -->“self.determinant(self)”
Any suggestions will be very appreciated任何建议将不胜感激
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.