1

我有一种情况,我在第一堂课中声明数组,然后将它传递给另一个对象,该对象打印该数组中元素的名称。它有效,但是当我输入“汽车”时。在 ReadCarNames ide 中不建议我使用“名称”?我在wing ide 4 pro中尝试过。我可以在方法 ReadCarNames 中投射汽车吗?

########################################################################
class MyClass:
    """"""

    #----------------------------------------------------------------------
    def __init__(self):
        cars=[]
        cars.append(Car('bmw'))
        cars.append(Car('audi'))
        reader=Reader()
        reader.ReadCarNames(cars)

########################################################################
class Car:
    """"""

    #----------------------------------------------------------------------
    def __init__(self,name):
        self.name=name



########################################################################
class Reader:
    """"""

    #----------------------------------------------------------------------
    def __init__(self):
        """Constructor"""
    def ReadCarNames(self,cars):
        for counter,car in enumerate(cars):

            print str(counter) +' '+ car.name
4

4 回答 4

3

见这里: http: //www.wingware.com/doc/edit/helping-wing-analyze-code

您的 IDE (Wing) 不确定是什么类型的对象cars,但您可以通过 assert 语句告诉它是什么car,它会按照您的要求进行自动完成。您可以将其视为仅在您喜欢的情况下为 Wing 的眼睛铸造类型。

class Reader:
    def __init__(self):
        """Constructor"""
    def ReadCarNames(self,cars):
        for counter,car in enumerate(cars):
            assert isinstance(car, Car)        # this trains Wing
            print str(counter) +' '+ car.name  # autocompletion will work here

或者,如果您不希望该断言一直触发,则可以将其包装在 Wing 的 SourceAssistant 拾取但 python 不会执行的“if 0”逻辑中。

if 0: assert isinstance(car, Car)

您目前无法告诉 Wing 列表/元组/等仅包含一种类型的对象及其是什么,但它在他们的计划中并且将使用类似的语法。

于 2011-02-28T15:35:07.340 回答
2

A good way to work in Wing IDE is to set a breakpoint, run to it, and then you'll get runtime-sourced analysis in the editor (in code that's on the active debug stack) and Debug Probe. This is illustrated in the "Static and Runtime Analysis" screen cast, second from last on http://wingware.com/wingide/code-intelligence

于 2012-02-09T15:10:28.603 回答
1

IDE 不知道从 enumerate 返回的类型,因此在这种情况下无法执行自动完成。它也不知道cars列表包含Car.

于 2011-02-28T15:12:53.043 回答
1

由于 Python 的动态特性,如果不运行代码,就不可能知道实例是什么类型,甚至它具有什么属性。例如,您的Car实例在实例化之前没有name属性,因此即使 IDE 以某种方式知道这car是一个Car实例,它也会花时间弄清楚它静态具有哪些属性。

这取决于您的 IDE,但某些 IDE(例如 Python 附带的 IDLE)会在您运行脚本后提供更好的结果。但是,在这种情况下,可能不会。

于 2011-02-28T15:16:22.613 回答