Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/315.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 从另一个类获取RequestHandler对象的上下文_Python_Google App Engine - Fatal编程技术网

Python 从另一个类获取RequestHandler对象的上下文

Python 从另一个类获取RequestHandler对象的上下文,python,google-app-engine,Python,Google App Engine,我想从其他应用程序引擎Python类中利用一个实用程序类方法: def parse_query_string_paramter(self, paramter, default=None): if self.request.get(paramter): # ... 从另一个类调用此方法时,我不确定如何传递原始请求的上下文,如下所示: import webapp2 from utilities import Utility class Search(

我想从其他应用程序引擎Python类中利用一个实用程序类方法:

def parse_query_string_paramter(self, paramter, default=None):

        if self.request.get(paramter):

            # ...
从另一个类调用此方法时,我不确定如何传递原始请求的上下文,如下所示:

import webapp2
from utilities import Utility


class Search(webapp2.RequestHandler):

    def get(self):

        utility = Utility()
        search_query = utility.parse_query_string_paramter('q')
        # ...
search_query = utility.parse_query_string_paramter(self, 'q')
下面返回的错误对我来说是有意义的,虽然我不清楚从哪里开始:

File "~/utilities.py", line 112, in parse_query_string_paramter
    if self.request.get(paramter):
AttributeError: 'NoneType' object has no attribute 'get'
更新:

多亏了Tim的解决方案,下面的更新代码现在对我有效:

def parse_query_string_paramter(self, context, paramter, default=None):

        if context.request.get(paramter):

            # ...
从调用类传递
self
,如下所示:

import webapp2
from utilities import Utility


class Search(webapp2.RequestHandler):

    def get(self):

        utility = Utility()
        search_query = utility.parse_query_string_paramter('q')
        # ...
search_query = utility.parse_query_string_paramter(self, 'q')

您需要将来自处理程序
Search
的请求作为实例化时实用程序类的参数传递,或者作为
parse\u query\u string\u parameter
方法的参数传递,它无法神奇地获取请求对象


顺便说一句,不清楚为什么您会有一个实用程序类,除非在请求期间实用程序实例保持某种状态,而您可能只需要一个函数。

谢谢Tim,作为参数传递成功了。我一直在使用Utility类来保存一些类中使用的常用方法,而不是在每个类中声明它们。从你的评论中我可以看出,为每个请求实例化类是多么浪费,不过,我需要并且将投资于一些软件设计教育。只需将方法定义为实用程序模块中的函数即可。然后只需使用
实用程序。parse_query_string_parameter
记住python确实会强制您在任何情况下使用对象/类。Thanks Tim,我现在正在重构,以使用继承而不是实用程序类,这给应用程序的管理增加了不必要的复杂性。