Python 知道传递给函数的参数是向量还是矩阵

Python 知道传递给函数的参数是向量还是矩阵,python,overloading,sage,Python,Overloading,Sage,我正在用Sage编写一个函数,它应该以不同的方式处理向量和矩阵 我不能使用isinstance函数,因为向量或矩阵的类型取决于元素的类型: sage: type(matrix([[1]])) <type 'sage.matrix.matrix_integer_dense.Matrix_integer_dense'> sage: type(matrix([[i]])) <type 'sage.matrix.matrix_symbolic_dense.Matrix_symbolic

我正在用Sage编写一个函数,它应该以不同的方式处理向量和矩阵

我不能使用
isinstance
函数,因为向量或矩阵的类型取决于元素的类型:

sage: type(matrix([[1]]))
<type 'sage.matrix.matrix_integer_dense.Matrix_integer_dense'>
sage: type(matrix([[i]]))
<type 'sage.matrix.matrix_symbolic_dense.Matrix_symbolic_dense'>
sage:type(矩阵([[1]]))
sage:类型(矩阵([[i]]))

区分向量和矩阵的最佳方法是什么?

在Sage源代码中尝试查找定义
matrix.dim
时意外发现了该解决方案

from sage.matrix.matrix import is_Matrix
from sage.structure.element import is_Vector

def myfunction(x):
    if is_Vector(x):
        # do something
    elif is_Matrix(x):
        # do something else
    else:
        raise TypeError("The argument must be vector or matrix")

只需使用
matrix.dim
@DavidZwicker检查尺寸,您能解释一下什么是
matrix.dim
,以及如何使用它吗?