我有一个带有一种方法的模块:
def find_inverse_matrix(C, log=False):
n = C.shape[0]
Cs = C.copy()
i = 0
C = np.matrix(np.eye(n))
B = np.matrix(np.eye(n))
J = set(range(n))
S = [0] * n
if log: print Cs
while i <= n-1:
if log: print '\nIteration', i
f = False
j = 0
ei = get_ek(i, n)
for j in J:
cj = get_ck(Cs, j)
alpha = (ei * B * cj)[0, 0]
if log: print 'alpha%s = %s' % (j, alpha)
if not(is_zero(alpha)):
f = True
break
if not(f):
exit('Inverse matrix is not exist')
J.remove(j)
S[j] = i
C[:, i] = Cs[:, j]
if log: print 'C%s:\n%s' % (i, C)
D = get_Dkz(i, B * C[:, i])
if log: print 'D%s:\n%s' % (i, D)
B = D * B
if log: print 'B%s:\n%s' % (i, B)
i += 1
if log: print '\n S = ', S
if log: print 'Result'
R = construct_matrix(B, S)
if log: print R
if log: print '\nCheck result:'
if log: print Cs * R
return R
如果我从这个文件调用这个方法,它会显示所有打印,但是如果我从另一个调用,所有打印都应该被禁止。现在我在每次打印之前都使用检查,但它是如何以“pythonic”风格简单而漂亮的呢?