Python Eclipse type casting intellisense work-arou

2019-07-31 13:18发布

问题:

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 to use that in the following way:

def do_something():
    myTop = TopClass()
    # create two items
    item1 = ItemClass()
    item1.name = "Tony"
    item2 = ItemClass()
    item2.name = "Mike"
    # add these to top class
    myTop.items.append(item1)
    myTop.items.append(item2)
    # up until this point, access class members is effortless as the 
    # IDE (Eclipse) automatically recognizes the type of the object
    # and can interpret the correct member variables. -- Awesome!

    # now let's try and do a for loop
    for myItem in myTop.items:
        myItem.name # <- I HAD TO TYPE the ".name" IN MANUALLY, 
                    # THIS IS ANNOYING, I could have misspelled
                    # something and not found out until
                    # I actually ran the script.

    # Hacky way of making this easier
    myItemT = ItemClass()
    for myItemT in myTop.items:
        myItemT.name = "bob" # <- Woah, it automatically filled in the
                            # ".name" part. This is nice, but I have the
                            # dummy line just above that is serving absolutely
                            # no purpose other than giving the
                            # Eclipse intellisense input.

Any opinions on the above? Is there a better way of making this work?

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



回答2:

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

class Foo(object):
    def __init__(self):
        self.name = None

class Bar(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.


回答3:

Issue 1: You could pass arguments to __init__

class ItemClass:
    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() # this is 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_with myItemT ...
# an indentation mistake
# This myItemT refers to the one outside for block
do_anotherthing_with myItemT ...