PyCharm 类型错误消息 [英] PyCharm Type error Message

查看:98
本文介绍了PyCharm 类型错误消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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.

推荐答案

致所有人,
在 G'g 很多之后,我找到了一个似乎在 PyCharm 中工作的解决方法.我说解决方法是因为(即使作为 Python 新手)我预计 Python 不需要显式类型转换,尤其是在使用从诸如 Numpy 之类的库导入的数据类型时.

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

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

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

这篇关于PyCharm 类型错误消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆