PyCharm Type error Message

2019-09-20 00:05发布

I'm a newbie trying out ML/DBN in Python (3.4) under PyCharm (Comm.Ed 4.5). The following definition

def make_thetas(xmin,xmax,n):
    xs = np.linespace(xmin,xmax,n)
    widths = (xs[1:] - xs[:-1])/2.0
    thetas = xs[:-1] + widths
    return thetas

throws the error "Class 'tuple' does not define 'sub', so the '-' operator cannot be used on its instance" on the - operator on the third line (widths = ....) Any ideas on how to get this code running under PyCharm - it works alright in the interactive Python window.
Thx.

1条回答
乱世女痞
2楼-- · 2019-09-20 01:00

To All,
After G'g a lot I have found a workaround that seems to work within PyCharm. I say workaround because (even as a Python newbie) I expected that Python does not need explicit typecasting, especially not when using datatypes imported from a lib such as Numpy.

    def make_thetas(xmin,xmax,n):
    xs = np.array(np.linspace(xmin,xmax,n))
    widths = (xs[1:] - xs[:-1])/2.0
    thetas = xs[:-1]+ widths
    return thetas

Using docstrings for type-hinting such as below did not work

def make_thetas(xmin,xmax,n):
    """
    @type xs: np.multiarray.ndarray
    """
    xs = np.linspace(xmin,xmax,n)
    widths = (xs[1:] - xs[:-1])/2.0  # Error Msg 1
    thetas = xs[:-1]+ widths         # Error Msg 2  Followup of Error 1
    return thetas

Error Msg 1: '-' Class 'tuple' does not define 'sub', so the '-' operator cannot be used on its instances Error Msg 2: 'widths' Expected type 'tuple', got 'float' instead
Maybe there are other possibilities of type-hinting that would work...
Thx

查看更多
登录 后发表回答