2012-01-14 103 views
3

我知道這已經很多次了,但我沒有找到正確的答案。
第一:我正在做一個簡單的數據庫系統(爲我自己),將運行沒有哈希等(至少現在)。現在我陷入了困境。Pythons的AttributeError:'NoneType'對象沒有屬性'錯誤'

import sys 
import os 

filename = "" 
database = "" 
path = "" 
table = "" 

class Nollty: 
    returns = 0 
    errors = 0 

    def __init__(self, filename, database): 
     self.filename = filename 
     self.database = database 
     self.path = self.filename + "databases/" + self.database 
     openfile = open(self.path + "/db_required", "r") 
     if not openfile.errors: 
      self.returns = 1 
     if not os.path.exists(self.path + "/db_required"): 
      self.returns = 0 
     openfile.close(); 

    def select(self, table): 
     errors = 0 
     self.table = table 
     openfile = open(self.path + "/" + self.table, "r") 
     if not openfile.errors: 
      errors = 1 
     if not os.path.exists(self.path + "/" + self.table): 
      errors = 0 
     openfile.close(); 


nollty = Nollty("", "test") 
if nollty.returns == 1: 
    print "Successfully connected to the database!" 

query = nollty.select("aaa_auto") 
if query.errors == 0: 
    print "Successfully chosen the table!" 

錯誤輸出是:

Traceback (most recent call last): 
File "/home/spotrudloff/Python/Nollty/nollty.py", line 40, in <module> 
if query.errors == 0: 
AttributeError: 'NoneType' object has no attribute 'errors' 

這個問題可能是,我是PHP程序員,我今天在幾個小時內學到的Python(所以我的想法仍然是「PHPy」)。

感謝您的所有回覆。

+2

使選擇函數返回一些東西... – JBernardo 2012-01-14 01:43:34

+0

請注意'openfile。錯誤「幾乎肯定不是你想象的那樣。它沒有設置爲錯誤代碼 - 它只是一個字符串,指定用於編碼的錯誤處理程序。在您的示例代碼中,它將始終具有值None。 (請參閱[文檔](http://docs.python.org/library/stdtypes.html#file.errors))。 – ekhumoro 2012-01-14 02:51:09

+0

+ 1你的-1,因爲我發現這有幫助。 – ryanjdillon 2013-02-08 16:35:21

回答

3

select()不返回一個明確的值,所以它有一個NoneType返回值。更改您的代碼,以便select()將返回1或0,具體取決於代碼的成功或失敗。

1

它看起來像select()沒有返回值,所以它默認爲一個NoneType(null)。或者(你可能打算做的),將查詢改爲nollty。

+0

名稱是'None',不是'null'。 'null'是一個在Java,C#,C++和['nil' in] Objective-C等語言中的文字「沒有[n對象]值」。在Python中,'None'是'NoneType'類型唯一的居民對象。要看到'None'與'null'不同(它仍然是一個對象),請考慮:'dir(None)'。 – 2012-01-14 02:06:46

+0

我明白這一點。我只是用它作爲一種快速的方式來描述什麼NoneType是用其他語言提供的東西(比如php,它使用null)。 – AlexanderZ 2012-01-14 03:12:35

4

您使用returnserrors作爲類變量似乎不是一個好主意。無論您創建多少個Nollty實例,只有一個這些變量中的每一個的實例。相反,這樣做:

def __init__(self, filename, database): 
    self.returns = 0 
    self.errors = 0 
    # rest of __init__ 

接下來,使用returns指示返回值似乎不是一個好主意,無論是。在Python中,通常會引發異常以指示構造函數中存在問題。這樣,通過忘記檢查returns,呼叫者不能簡單地忽略問題。

同樣,使用select()的例外來指示參數存在問題。我的建議是消除returnserrors

你也不會返回任何值從select可言,所以這就是爲什麼query最終被None(Python中,None是一個特殊值)。你要麼從select()中返回一些有用的東西,要麼不返回一個有用的值給它的結果。

0

您的select方法中的errors變量是一個局部變量。您需要明確設置類錯誤變量。另外,正如其他人所建議你的select方法沒有return語句。從你的代碼看起來你試圖訪問nollty的錯誤變量,但是你在邏輯上是None的查詢變量中檢查它。

根據你的目標,你有兩個選擇。在select方法返回之前,從select或set self.errors = errors中返回錯誤。或者如果你喜歡,就做兩個,如下所示。通常,如果選擇失敗,您可能會返回False,如果成功,您可能會返回True,但這不是必需的。

class Nollty: 
    ... 
    def select(self,table): 
     ... 
     self.errors = errors 
     return errors 

errs = nollty.select("aaa_auto") 
if nollty.errors == 0: 
    print "Successfully chosen the table!" 

##functionally equivalent 
#if errs==0: 
# print "Successfully chosen the table!" 
+0

好吧,我已經解決了使用返回的問題。我也用例外重寫了__returns__。謝謝你們。 :) – 2012-01-14 11:49:42

相關問題