As self.axis is only 0 or 1, perhaps you could refactorize the "if axis == 1 /else" and "if axis == 0" together.