Problem with lack of autocomplete / cast in python

I have a situation where in the first class I declare an array and I pass it to another object that prints the name of the elements in this array. It works, but when I entered "car". at ReadCarNames ide doesn't offer me a "name"? I try this in wing ide 4 pro. Can I throw a car into the ReadCarNames method?

######################################################################## 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
source share
4 answers

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

Your IDE (Wing) does not know for sure what type of objects are in cars , but you can say that car has an assert statement and it will do autocomplete exactly the way you want it to. You can view it as a molding for the wing eyes, only if you want.

 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 do not want this statement to be disabled all the time, you can wrap it in the if logic that the Wing SourceAssistant will pick up, but python will not execute.

 if 0: assert isinstance(car, Car) 

Currently, you cannot tell Wing that the / tuple / etc list contains only one type of object and what it is, but it is in their plans and will use the same syntax.

+2
source

A good way to work in the Wing IDE is to set a breakpoint, run it, and then you will get a runtime analysis in the editor (in the code that is in the active debugging stack) and Debug Probe. This is shown on the Static and Temporal Analysis screen, second from last at http://wingware.com/wingide/code-intelligence

+2
source

The IDE does not know the type returned from the enumeration, and therefore cannot autocomplete in this situation. He also does not know that the list of cars contains Car .

+1
source

Due to the dynamic nature of Python, it is not possible to know what type of instance or attributes it has without running code. For example, your Car instances do not have a name attribute until they are created, so even if the IDE somehow knew that Car an instance of Car , it would have a hell of a time figuring out what attributes it would have statically.

It depends on your development environment, but some IDEs (such as the IDLE that ships with Python) give better results after running the script. In this case, probably not.

+1
source

Source: https://habr.com/ru/post/1393219/


All Articles