Problem with lack of autocomplete/casting in python

时光怂恿深爱的人放手 提交于 2019-12-14 02:26:52

问题


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

回答1:


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.




回答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




回答3:


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.




回答4:


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.



来源:https://stackoverflow.com/questions/5143646/problem-with-lack-of-autocomplete-casting-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!