2013-07-08 63 views
1

嗨我正在python上做一個程序,我在添加一個全局變量到我的程序時遇到了問題,所以我只是要發佈我的代碼並告訴你我是如何嘗試去做的。Python中的全局變量aa

所以這是我的課:

import globalvariables 

class Bus : 

def __init__(self, Number, Capacity, Destination, Seats): 
    self.Bus_Number = Number 
    self.Bus_Capacity = Capacity 
    self.Bus_Destination = Destination 
    self.Seats_taken = Seats 

def Book(self): 
    self.Seats_taken = Seats + 1 

def ShowBus(self): 
    return (str(self.Bus_Number) + ", " + str(self.Bus_Capacity) + ", " + str(self.Bus_Destination) + ", " + str(self.Seats_taken)) 

,這是我爲全局變量

Seats = 0 

和模塊這就是我試圖運行:

import Transport 
import globalvariables 

Big_Red = Transport.Bus(1, 50, "NYC", 0) 
Big_Red.Book() 

print(Big_Red.ShowBus()) 

我得到這個錯誤:

Traceback (most recent call last): 
    File "D:\Python\Assignment 3\Tester.py", line 5, in <module> 
    Big_Red.Book() 
    File "D:\Python\Assignment 3\Transport.py", line 14, in Book 
    self.Seats_taken = Seats + 1 
NameError: global name 'Seats' is not defined 

回答

1

變量Seats位於__init__函數的本地,不能在其外部訪問。

所以,

self.Seats_taken = Seats + 1 

應該是:

self.Seats_taken = self.Seats_taken + 1 

或:

self.Seats_taken += 1 

而不是使用內部類的全局變量,你應該使用類的屬性:

class Bus : 
    seats = 50 #shared across all instances 
    def __init__(self): 
     #code 
    def Book(self): 
     self.Seats_taken = self.seats + 1 
+0

感謝布拉赫,但如果我沒有想使全局變量的模塊和使用,在我的車類,我如何做呢? – Panthy

+0

@Panthy你在哪個文件中定義了'Seats'變量? –

+0

@Panthy如果它在'globalvariables'內,那麼使用'globalvariables.Seats'。你最好創建一個類屬性'Seats'。 –

1

Globals should be avoid。如果你還希望它是:

def Book(self): 
    self.Seats_taken = globalvariables.Seats + 1 
0

當你import globalvariables,您可以訪問由模塊名限定名稱:globalvariables.Seats。要將Seats導入另一個模塊的名稱空間,請使用from globalvariables import Seats。 (在絕望的情況下,您可以導入模塊中的所有名稱:from globalvariables import *,但通常不需要此名稱。)

定義函數時,它具有自己的本地名稱空間。它包含了所有函數的參數。

Seats = 100 

def foo(Seats): 
    # returns the value of variable named Seats 
    # defined within "def foo", *not* defined by "Seats = 100" 
    return Seats 

print foo(200) # prints 200 
print foo() # fails because Seats are not set 

要初始化函數參數,使用默認值:

def foo(seats=0): 

print foo() # prints 0 
print foo(55) # prints 55 

此外,global variables are evil。全局常量是好的。

您想使用全局變量來跟蹤所採用的座位。你會,如果你將其封裝成一個類,只允許合理訪問過好多了,不允許隨意設置的值,如果需要登錄訪問,等:

class SeatsDispenser(object): 
    def __init__(self, initial_count): 
    self.count = initial_count 

    def allocate(self, number_of_seats): 
    self.count -= number_of_seats 
    if self.count < 0: 
     raise ValueError("Overcommitted!") 

    def seats_left(self): 
    return self.number_of_seats 

命名變量,類常量,並且具有相同標題情況的功能是不切實際的。通常變量是lower_case,函數是lowerCamelCase,類是TitleCamelCase,常量是ALL_CAPS。

合理的一段代碼如下所示:

import constants # modules are usually lower case 
import transport 

def Bus(object): 
    def __init__(self, number, capacity, seats=constants.SEATS): 
    self.number = number 
    self.capacity = capacity 
    self.seats = seats 

big_red = Bus(constants.NYC_BUS_NUMBER, 50, 25) 
default_blue = Bus(1, 20) # seats not passed, the default value is used 

seats_dispenser = SeatsDispenser(100) 
seats_dispenser.allocate(big_red.count) 
seats_dispenser.allocate(default_blue.count) 
print seats_dispenser.seats.left()