Python Eclipse 类型转换智能感知解决方法

发布于 2024-09-30 12:55:50 字数 1462 浏览 5 评论 0原文

假设我有以下两节课。

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.

对上述内容有什么意见吗?有没有更好的方法来完成这项工作?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

陈年往事 2024-10-07 12:55:50

我可能拼错了一些东西,直到我实际运行脚本才发现。

短视和虚假。

可能拼错了某些内容,但直到您因为没有进行单元测试而遭受诉讼时才发现。

“实际运行脚本”并不是您了解自己是否做得正确的时间。

使用或不使用 Eclipse 智能感知键入代码都不是您发现问题的时候。

运行脚本并不是当你发现问题的时候。

单元测试是发现问题的时候。

请停止依赖 Eclipse 智能感知。请开始单元测试。

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.

∝单色的世界 2024-10-07 12:55:50

IntelliSense 只是无法知道您想让它知道什么。想想这段代码:

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.

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.
挽清梦 2024-10-07 12:55:50

问题 1:您可以将参数传递给 __init__

class ItemClass:
    def __init__(self, name):
        self.name = name

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

问题 2:让编辑器为您工作,而不是为编辑器构建代码。

    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: 
        .....

由于不同的错误,这可能会在以后引起问题,并且编辑器不会为您提供帮助。

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

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 ...  
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文