2013-08-01 77 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