Q:

Python: How to execute only parts of a function?

I have defined a class which contains some basic matrix functions. My function for transposing a matrix looks like this:
def transpose(self):
    '''
    Transpose a matrix.
    '''
    C = Z ## creating a new zero matrix
    for i in range(0, self.rows):
        for j in range(0, self.cols):
            C.matrix[i][j] = self.matrix[j][i]

    ## printing the resultant matrix
    C.show()
    return C

So when I call this function from the interpreter, it prints the result after execution (because of the show() function). 
However, when I call this function from another function in the same class, I don't want the matrix to be printed, that is, I don't want the C.show() part to execute.
Is there any way to do this? I was thinking on the lines of __name__=="__main__" but that doesn't really apply here it seems.
Thanks!

A:

Just add another, default, parameter to the function and put the print in an if:
def transpose(self, print_matrix=True):
    '''
    Transpose a matrix.
    '''
    C = Z ## creating a new zero matrix
    for i in range(0, self.rows):
        for j in range(0, self.cols):
            C.matrix[i][j] = self.matrix[j][i]

    ## printing the resultant matrix
    if print_matrix:
        C.show()
    return C

As it has a default value you don't need to change any current method calls, but you can add another parameter to your new one. Call it as transpose(False), if you don't want to print.

