PyCharm getitem warning for functions with arrays

后端 未结 2 1622
野性不改
野性不改 2021-02-04 03:13

I\'m getting code inspection warnings from PyCharm. I understand the logic, but I\'m not clear on the appropriate way to fix it. Say I have the following example function:

相关标签:
2条回答
  • 2021-02-04 03:53

    TL;DR Cast it using list()

    Its late, still,

    I had similar problem with some other code.

    I could solve it by something similar to

    def get_ydata(xdata):
        ydata = list(xdata ** 2)
        for i in range(len(ydata)):
            print ydata[i]
        return ydata
    

    Consider the accepted answer. Comments on my answer are valid.

    0 讨论(0)
  • 2021-02-04 04:04

    Pycharm has type hinting features that may be of use.

    For example in this case, the following code makes the errors go away:

    import numpy as np
    
    def get_ydata(xdata):
        ydata = xdata ** 2  # type: np.ndarray
        for i in range(len(ydata)):
            print(ydata[i])
        return ydata
    

    Recent python versions also include support for type annotations

    import numpy as np
    def get_ydata(xdata: np.ndarray):
        ...
    
    0 讨论(0)
提交回复
热议问题