1

次の2つのクラスがあるとします。

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.

上記について何か意見はありますか?これを機能させるためのより良い方法はありますか?

4

3 に答える 3

1

実際にスクリプトを実行するまで、何かのスペルを間違えていた可能性があります。

近視眼的で虚偽。

単体テストを行っていないため、何かのスペルを間違えたとしても、訴訟に耐えるまで気づかなかった可能性があります。

「実際にスクリプトを実行した」ということは、正しく実行したかどうかを知るときではありません。

Eclipse インテリセンスを使用して、または使用せずにコードを入力しても、問題が見つかるわけではありません。

スクリプトを実行するのは、問題を見つけるときではありません。

単体テストは、問題を見つけるときです。

Eclipse インテリセンスに頼るのをやめてください。単体テストを開始してください。

于 2010-11-05T23:38:41.807 に答える
1

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.
于 2010-11-06T00:51:34.213 に答える
0

問題 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 ...  
于 2010-11-05T23:42:08.130 に答える