python 中缺乏自动完成/转换的问题

发布于 2024-10-19 23:59:02 字数 1091 浏览 0 评论 0原文

我遇到一种情况,在第一类中我声明数组,然后将其传递给另一个对象,该对象打印该数组中元素的名称。它有效,但是当我输入“汽车”时。在 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

I have a situation where in first class I declare array, and I pass it to another object, which prints name of elements in this array. It works, but when I input 'car.' in ReadCarNames ide doesn't suggest me 'name' ? I'm trying it in wing ide 4 pro. Can I cast car in method 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

美人迟暮 2024-10-26 23:59:02

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

您的 IDE (Wing) 不确定汽车中的对象是什么类型,但您可以告诉它什么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 拾取的“if 0”逻辑中,但 python 不会执行。

if 0: assert isinstance(car, Car)

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

See here:
http://www.wingware.com/doc/edit/helping-wing-analyze-code

Your IDE (Wing) doesn't know for sure what type of objects are in cars, but you can tell it what car is with an assert statement and it will do autocompletion exactly how you want it to. You can view it as casting the type for Wing's eyes only if you like.

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

or if you don't want that assert firing off all the time, you can wrap it in 'if 0' logic that Wing's SourceAssistant picks up but python will not execute.

if 0: assert isinstance(car, Car)

You currently cannot tell Wing that a list/tuple/etc only contains one type of object and what it is, but it is in their plans and will use similar syntax.

往事风中埋 2024-10-26 23:59:02

在 Wing IDE 中工作的一个好方法是设置一个断点,运行到它,然后您将在编辑器(在活动调试堆栈上的代码中)和调试探针中获得运行时源分析。这在 http://wingware.com 上倒数第二个“静态和运行时分析”屏幕截图中进行了说明/wingide/代码智能

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

情场扛把子 2024-10-26 23:59:02

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

The IDE does not know the type that is being returned from enumerate, and therefore cannot perform autocomplete in that situation. It also does not know that the cars list contains Car.

绝不服输 2024-10-26 23:59:02

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

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

Due to the dynamic nature of Python, it's impossible to know what type an instance is, or even what attributes it has, without running the code. For example, your Car instances don't have a name attribute until they are instantiated, so even if the IDE somehow knew that car was a Car instance, it would have a devil of a time figuring out what attributes it would have statically.

It depends on your IDE, but some IDEs (such as the IDLE that comes with Python) will give better results after you run your script. In this case, though, probably not.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文