【问题标题】:PyCharm Type error MessagePyCharm 类型错误消息
【发布时间】:2015-07-05 14:45:55
【问题描述】:

我是一名新手,在 PyCharm (Comm.Ed 4.5) 下尝试使用 Python (3.4) 中的 ML/DBN。 以下定义

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

抛出错误 “类 'tuple' 没有定义 'sub',因此不能在其实例上使用 '-' 运算符” 在第三行的 - 运算符上(宽度 = ....) 有关如何在 PyCharm 下运行此代码的任何想法 - 它在交互式 Python 窗口中运行良好。
谢谢。

【问题讨论】:

    标签: python pycharm


    【解决方案1】:

    致所有人,
    在 G'g 很多之后,我发现了一个似乎在 PyCharm 中工作的解决方法。我说解决方法是因为(即使作为 Python 新手)我希望 Python 不需要 explicit 类型转换,尤其是在使用从诸如 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
    

    使用文档字符串进行类型提示(如下所示)不起作用

    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
    

    错误消息 1:'-' “元组”类没有定义“sub”,因此不能在其实例上使用“-”运算符 错误消息 2:“宽度” 预期类型为“元组”,改为“浮点”
    也许还有其他可行的类型提示的可能性...
    谢谢

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-07-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-30
      相关资源
      最近更新 更多