2014-01-23 20 views
2
from random import randint 
shifts = [4, 4.2, 5, 6, 7] 
days_names = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday'] 
workers_names = ['Itai', 'Or', 'Reut', 'Kuka', 'Aviel'] 
counter = 1 

def shift_arrange(worker): 
    for day in days.values(): 
     counter+=1 
     global avilable_shifts 
     avilable_shifts = check_avilable_shifts(day) 
     if not random_shifte_selector(worker,day): soft_reset(worker) 

我設置計數器作爲一個全局變量,當我嘗試運行這段代碼我得到的局部變量錯誤:局部變量引用時我將它的全球

Traceback (most recent call last): 
    File "C:\Or\mypy\shift creator\shift cretor.py", line 144, in <module> 
    for w in workers.values(): shift_arrange(w) 
    File "C:\Or\mypy\shift creator\shift cretor.py", line 105, in shift_arrange 
    counter+=1 
UnboundLocalError: local variable 'counter' referenced before assignmen 

我看到有些人在這裏問這個問題,他刪除了他的pyc文件或其他東西(我不知道它是什麼),它的工作很好。爲什麼會發生?它不會發生在程序中的其他變量。

感謝或

回答

4

你需要聲明一個全局變量

def shift_arrange(worker): 
    global counter 
    for day in days.values(): 
     counter+=1 
     ... 

既然你在該範圍內修改counter,蟒蛇把它當作一個局部變量,除非你聲明爲global。如果你只需要閱讀它,那沒有必要。

考慮以下幾點:

這工作:雖然這確實

c = 0 
def f(): 
    print c 
    c = 1 
f() 

c = 0 
def f(): 
    global c 
    print c 
    c = 1 
f() 
print c # prints 1, f() modified the global value 
+0

但是,python不自動查找全局變量,如果它沒有在本地找到它?我的意思是,爲什麼它的作品只用於閱讀? 其平均蟒蛇知道如何找到它... –

+1

但你在全球範圍內做到了這一點... – shx2

+0

嗨我現在看到它。它讓我閱讀,但每當我需要寫它時,我都需要說它的全局,然後它從全局定義中獲取。 我不明白它的好處,但謝謝:-) –

1

有很多值得

c = 0 
def f(): 
    print c 
f() 

雖然這並不在這裏,但是一般來說,python都有名稱和分配給它們的東西。該名稱可以在本地或全球範圍內。

對於讀取操作,python查看本地範圍,然後查看全局範圍,並使用它找到的第一個(或者如果沒有,則返回錯誤)。

對於寫入... python需要知道把它放在哪裏。通常,它會做的是查看本地範圍,如果它不在那裏,則在那裏創建一個變量並賦值。這會隱藏全局變量。你可以讓它看起來像全局變量,如果它存在的話就使用它 - 但這可能是不可取的&。所以,你需要一種方法來告訴Python使用全局變量,而不是它存在(然後,如果它不存在,它會創建)。

這會導致一些奇怪的行爲,有時也是如此。除了以前的答案...

c = 0 

# Passes. We are just looking up the global variable. 
def f1(x): 
    return x + c 

# Passes, but may not be as expected. Sets a local variable c to a value, does not 
# modify the global one. 
def f2(x): 
    c = x 

# Correct way to do the above; now it sets the global variable. 
def f3(x): 
    global c 
    c = x 

# What if you mix them? 
def f4(x): 
    c = c + x 
# This fails. What happens is that first it sees that it's writing to c, and it's 
# not marked global, so it assigns c to the local space. Then it tries to dereference 
# it. Since we've marked it local, it masks the global one, and since it doesn't 
# have a value, it throws an error. c += x works the same way and also fails. 

# However, this works, though is just as equally wrong: 
def f5(x): 
    d = c 
    c = d + x 
# This one works, because we copy the value of the global c into the local d. 
# Then, the second line assigns a local c to addition of local d and x. 
# But does not update the global. 

# Each line is separate though: 
def f6(x): 
    d = c 
    c = c + 1 
# You might think that d=c already made c local for the whole function. But it doesn't 
# work like that. The first line just looks up the value for c, which it finds 
# globally, but then forgets about it - it cares about the object c is the name of, 
# not the name c itself. The second line still fails. 
+0

這解釋了很多。但是我不明白這個主意。爲什麼python不以相同的方式讀取或寫入? 爲防止丟失數據?它可以在讀取方法中發生('d = c')。 這隻海狸背後的邏輯......? –

+1

好吧,它_does_使用相同的方式。只是C等,變量_always_存在,所以你只需要找到它,然後使用它,無論它是讀還是寫,以及如果不這樣做,都會出錯。在Python中,讀取操作是這樣的,但對於寫入來說,它可能存在也可能不存在,如果不存在,你應該創建它,並且你需要知道把它放在哪裏。 Python爲您提供了兩種選擇:本地框架(這是默認框架)或全局框架。這兩個只是解耦。 –