Python Eclipse类型转换intellisense解决方案 [英] Python Eclipse type casting intellisense work-around

查看:122
本文介绍了Python Eclipse类型转换intellisense解决方案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我有以下两个类。

class TopClass:
    def __init__(self):
        self.items = []
class ItemClass:
    def __init__(self):
        self.name = None

我想以下列方式使用:

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?

推荐答案

IntelliSense只是不知道你想要什么。想想这个代码:

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.

这篇关于Python Eclipse类型转换intellisense解决方案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆