Skip to content Skip to sidebar Skip to footer

Python Eclipse Type Casting Intellisense Work-around

Say I have the following two classes. class TopClass: def __init__(self): self.items = [] class ItemClass: def __init__(self): self.name = None And I want

Solution 1:

I could have misspelled something and not found out until I actually ran the script.

Short-sighted and false.

You could have misspelled something and never found out until you endured a lawsuit because you did no unit testing.

"actually ran the script" is not the time when you learn if you did it right.

Typing code with or without Eclipse intellisense is not when you find the problems.

Running the script is not when you find the problems.

Unit testing is when you find the problems.

Please stop relying on Eclipse intellisense. Please start unit testing.

Solution 2:

IntelliSense just can't know what you want it to know. Think of this code:

classFoo(object):
    def__init__(self):
        self.name = NoneclassBar(object):
    def__init__(self):
        self.blub = None

bar1 = Bar()
bar2 = Bar()
bar1.blub = 'joe'
bar2.blub = 'jim'

items = [bar1, bar2]

each = Foo()
for each in items:
    each.name = 'Wha?'# here Eclipse also filled in the name attribute,# although each is never a Foo in this loop.# And funny, this is perfectly valid Python.# All items now have a name attribute, despite being Bars.

Solution 3:

Issue 1: You could pass arguments to __init__

classItemClass:def__init__(self, name):
        self.name = name

item1 = ItemClass("tony") # this is better

Issue 2: Make editor work for you and not structure your code for editor.

    myItemT = ItemClass() # thisis misleading !!

    # myItemT here is not same as above. What is some one changes this to x? 
    for myItemT in myTop.items: 
        .....

This can cause an issue later on due to different mistake and editor will not help you there.

myItemT=ItemClass()for myItemT in myTop.items:do_something_withmyItemT...# an indentation mistake# This myItemT refers to the one outside for blockdo_anotherthing_withmyItemT...

Post a Comment for "Python Eclipse Type Casting Intellisense Work-around"