2013-08-01 76 views
1

我是python的新手,試圖定義一個函數,然後在Google App Engine中使用它 - 但我不斷收到錯誤「Error:global name'cache_email_received_list'is not defined」when我嘗試執行該功能。任何幫助將不勝感激,謝謝。在Google App Engine中定義Python函數

這裏是我的功能:

class EmailMessageHandler(BaseHandler2): 
def cache_email_sent_list(): #set email_sent_list to memcache 
    email_sent_list = db.GqlQuery("SELECT * FROM EmailMessage WHERE sender =:1 ORDER BY created DESC", user_info.username) 
    if email_sent_list: 
     string1 = "email_sent_list" 
     email_sent_list_cache_id = "_".join((user_info.username, string1))     
     memcache.set('%s' % email_sent_list_cache_id, email_sent_list, time=2000000)  
     logging.info('**************email_sent_list added to memcache*********')  

這裏就是我想稱之爲:

if email_received_list is None and email_sent_list is not None: 
    params = { 
    'email_sent_list': email_sent_list,   
    } 
    cache_email_sent_list() 
+0

我沒有看到'cache_email_received_list'定義或在任何地方使用。 –

回答

1

cache_email_sent_list()是類EmailMessageHandler的方法therfore該方法需要在自AA參數來傳遞,因此它看起來就像這樣:

class EmailMessageHandler(BaseHandler2): 
    def cache_email_sent_list(self): #set email_sent_list to memcache 
     email_sent_list = db.GqlQuery("SELECT * FROM EmailMessage WHERE sender =:1 ORDER BY created DESC", user_info.username) 
     if email_sent_list: 
      string1 = "email_sent_list" 
      email_sent_list_cache_id = "_".join((user_info.username, string1))     
      memcache.set('%s' % email_sent_list_cache_id, email_sent_list, time=2000000)  
      logging.info('**************email_sent_list added to memcache*********') 

然後,當你從類EmailMessageHandler中調用它,你必須這樣做:

self.cache_email_sent_list() 

然而,如果你從類的外部調用它EmailMessageHandler您需要首先創建一個實例,然後使用調用它:

instanceName.cache_email_sent_list() 
+0

謝謝你,Abarnert和Alec--解決了這個問題。就我自己的知識而言,你能給我一個例子,說明如何從EmailMessageHandler之外調用它嗎? Thankyou –

+0

@MattEllis如Roger的回答所示,在類的外部使用非靜態方法創建該類的一個實例:myInstance = MyClass()然後調用您所使用的方法myInstance.myMethod()但是,如果'EmailMessageHandler'是一個處理程序,我猜你不太可能創建它的一個實例。因此你有2個選擇;使該方法成爲靜態方法 - 請參閱Rogers示例。或者把課堂以外的方法,並使其成爲一個全球性的功能。 –

+0

除非你的處理程序需要保存狀態(有實例變量),否則真的不需要在一個類中包裝chache_email_sent_list()。我傾向於將其作爲全球職能來實施。除非你的處理程序是你想利用繼承的處理程序的層次結構的一部分。在這種情況下,將它作爲一個類中的靜態方法來實現是有意義的。希望這可以幫助。 – Roger

0

問題無關與GAE。

問題是,您已將cache_email_sent_list定義爲類EmailMessageHandler的方法,但您試圖將其稱爲頂級函數。你不能那樣做。您需要有一個EmailMessageHandler的實例來調用它。

如果你想從EmailMessageHandler另一種方法調用它,該實例應該作爲self。例如:

self.cache_email_sent_list() 

如果您嘗試從別處調用它,則由您決定應該調用它的實例。例如:

handler_passed_as_param_to_this_function.cache_email_sent_list() 

注意,你的錯誤消息爲cache_email_received_list,但你的代碼只有cache_email_sent_list。我在猜測你有並行代碼,對於這兩種情況都有相同的錯誤,但我當然可能猜測是錯誤的 - 在這種情況下,你必須實際向我們顯示與顯示的錯誤一起出現的代碼,或者與您的顯示的代碼去的錯誤...

+0

謝謝Abarnet-我知道這是基本的Python,但如果我要在另一個處理程序中調用此函數,那麼我只需要輸入:EmailMessageHandler.cache_email_sent_list()? - 謝謝 –

+0

@MattEllis:不。你不會在類上調用正常的方法。你在類的_instances_上給他們打電話。你需要在某個地方有一個EmailMessageHandler實例。它可能是一個局部變量,當前函數的參數,當前方法的'self'參數,某個其他對象的全局成員屬性等等,但無論如何,你必須有一個EmailMessageHandler '如果你想用'EmailMessageHandler'做一些事情;否則,它沒有任何意義。 – abarnert

1

,正如除了以前的答案:在您的文章,你定義cache_email_sent_list()爲一個在類定義中定義的函數,這是行不通的。我認爲你很困惑實例方法,靜態方法和函數。這三者之間有明顯的區別。

所以,作爲一個程式化例如:

# instance method: 
class MyClass(MySuperClass): 
    def my_instance_method(self): 
     #your code here 

# call the instance method: 
instance = MyClass() # creates a new instance 
instance.my_instance_method() # calls the method on the instance 

# static method: 
class MyClass(MySuperClass): 
    @staticmethod # use decorator to nominate a static method 
    def my_static_method() 
     #your code here 

# call the static method: 
MyClass.my_static_method() # calls the static method 

# function 
def my_function(): 
    # your code here 

# call the function: 
my_function() # calls your function 

縮進是Python語法的一部分,並且決定了解釋如何處理你的代碼。它需要一些習慣,但是一旦你掌握了它,它確實非常方便,並且使你的代碼非常易讀。我認爲你原來的帖子中有一個縮進錯誤。只需爲方法cache_email_sent_list()添加正確的縮進,並在EmailMessageHandler的實例上調用它,那麼您就很好。

+0

Roger,你能告訴我更多關於裝飾者/ @ staticmethod的信息嗎? - 感謝 –

+0

查看來自Bruce Eckel的http://www.artima.com/weblogs/viewpost.jsp?thread=240808。 – Roger