开发者

Function call in Google App Engine - Python

In main.py I have a display code that repeats several times. So I created a f1.py and placed the code in f1.py as a function display(query) and I am calling it from main.py as f1.display(query).

But display(query) has the line

self.response.out.write(
# some code
)

and I get the error message:

self.response.out.write(
NameError: global name 'self' is not defined

I tried to import from google.appengine.ext import webapp inside the display(query) function but tha开发者_StackOverflowt did not help.

What am I doing wrong and how can I fix it? Thanks!


self is a conventional name for the first argument passed to a class instance's methods. A class instance will pass a reference to itself as the first argument to all of it's methods when they are called. It's common practice to name the first parameter for instance methods self.

So, when you factored out part of your method's (presumably get or post on a sublcass of webapp.RequestHandler) functionality to another function, you can no longer refer to self and get the response property.

The easiest way to fix this would probably be to return the output you wish to write to the response in the function. Then you can call self.response.out.write with your function's return value from within in the method as you did before the refactor.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜