1

私はこれで3時間戦っています。

ETA- これについて言及する必要がありましたが、このクラスの目的上、グローバル変数の使用は許可されていません。

関数 main() 内で、関数全体が初めて実行される場合にのみ、関数 firstPass を実行したいと考えています。firstPass 関数は、いくつかの変数を初期化し、初めて見なければ意味のない情報を出力します。

私は正当に持っています:

#Initialize variables that need to run before we can do any work here
count = 0

def firstPass():
    x = 5
    y = 2
    print "Some stuff"
    count = count + 1
    print "Count in firstPass is",count
    return count,x,y

def main ():
    print "Count in main is",count
    if count == 0:
        firstPass()
    else:
        #Do a whole bunch of other stuff that is NOT supposed to happen on run #1
        #because the other stuff uses user-modified x and y values, and resetting
        #to start value would just be self-defeating.

main()

これは最初のパスでは正しく返されますが、その後のパスでは次のように返されます。

Count in main is 1

これは、他の関数内でユーザーが変更した x 値と y 値の問題でもあります。ここではそれらを変更していませんが、後でコード内の関数間で複数の値を渡す必要があるため、それらを含めましたが、例のためにここにそれらを置くことができたときに、誰がそれらすべてを読みたいと思っているのでしょうか.. .

という印象を受けました

return [variable]

変数の CURRENT (つまり、その変数が現在の関数内で変化したもの) の値を他の後続の関数に渡しました。私の理解が間違っているか、単に間違っているだけです。

4

4 に答える 4

2

あなたの理解returnは間違っています。

返された値は、呼び出し元の名前空間に挿入されるだけでなく、受信する必要があります。同じ名前で受け取る必要もありません。

count,a,b = firstPass()

countまた、グローバルから取得するのではなく、現在をパラメーターとして関数に渡すことをお勧めします。全体的にスタイルが良くなり、関数が理解しやすくなります。

于 2013-03-27T03:41:18.597 に答える
2

あなたがする必要があります:

def firstPass():
    global count

count更新する変数を取得します。

キーワードを使用して、値を同じ名前のローカル変数に保存するのではなくglobal、グローバル変数にロードして保存するようにインタープリターに指示します。count例えば:

を使用する関数と使用しない関数の 2 つの関数を定義しますglobal

>>> a = 0
>>> def foo():
...   a += 1
...   return a
>>> def bar():
...   global a
...   a += 1
...   return a

モジュールを使用して両方の関数を逆アセンブルdisすると、違いが明らかになります。

>>> import dis
>>> dis.dis(foo)
  2           0 LOAD_FAST                0 (a)
              3 LOAD_CONST               1 (1)
              6 INPLACE_ADD         
              7 STORE_FAST               0 (a)

  3          10 LOAD_FAST                0 (a)
             13 RETURN_VALUE        

>>> dis.dis(bar)
  3           0 LOAD_GLOBAL              0 (a)
              3 LOAD_CONST               1 (1)
              6 INPLACE_ADD         
              7 STORE_GLOBAL             0 (a)

  4          10 LOAD_GLOBAL              0 (a)
             13 RETURN_VALUE        
于 2013-03-27T03:24:48.750 に答える
0

おそらく、クラスの使用を検討したいと思うでしょう。状態を維持するための優れたコンテナーです。

def firstPass():
    x = 5
    y = 2
    print "Some stuff"
    count = count + 1
    print "Count in firstPass is",count
    return count,x,y        

class MyActivityManager(object):
    def __init__(self):
        """Initialize instance variables"""
        self.run_count = 0

    def run(self):
        if not self.run_count:
            results = firstPass()
            self.run_count += 1
            # consider saving results in the instance for later use
        else:
            # other stuff


if __name__ == "__main__":
    runner = MyActivityManager()
    runner.run()
于 2013-03-27T03:29:22.320 に答える
0
def firstPass(count):
    x = 5
    y = 2
    print "Some stuff"
    count = count + 1
    print "Count in firstPass is",count
    return count,x,y

def main():
    count = 0 # initialize it here; you're only running main once
    print "Count in main is",count
    if count == 0:
        count, x, y = firstPass(count) # sets the values from firstPass()
    else:
        #Do a whole bunch of other stuff that is NOT supposed to happen on run #1
        #because the other stuff uses user-modified x and y values, and resetting
        #to start value would just be self-defeating.

main()

ただし、count以外で初期化する場合は、次main()を使用しますmain(count)

#Initialize variables that need to run before we can do any work here
count = 0

def firstPass(count):
    x = 5
    y = 2
    print "Some stuff"
    count = count + 1
    print "Count in firstPass is",count
    return count,x,y

def main (count):
    print "Count in main is",count
    if count == 0:
        firstPass()
    else:
        #Do a whole bunch of other stuff that is NOT supposed to happen on run #1
        #because the other stuff uses user-modified x and y values, and resetting
        #to start value would just be self-defeating.

main(count) # pass it as an argument

ただし、一度しか呼び出していない場合はmain()、コードと一般的な構造を次のように書き直すことができます。

def main():
    count = 0
    count, x, y = firstPass(count) # no need to check if it is 0
    # do everything else

def firstPass(count):
    x, y = 5, 2 # multiple assignment, same as x = 5; y = 2
    print "some stuff"
    count += 1 # shorthand for count = count + 1
    print "Count in firstpass is", count
    return count, x, y

main()
于 2013-03-27T17:37:53.323 に答える