我已经在Python中为Google App Engine构建了一个Web应用程序。这是成熟的代码,我已经运行了很多次没有任何问题。然而,当我今天早上做出一些改变时,整个地方都出现了神秘的空白错误。在弄清整个文本文件(在TextWrangler中)并将我的设置更改为自动展开选项卡之前,我弄清楚了发生了什么并手动删除了一些。我想我已经赶走了所有的错误。当我在我的文件上运行python -m tabnanny时,我没有报告错误。
但是,之前一大堆代码中出现了一个新的错误。我不知道错误是在类或它正在调用的类中,所以我在下面包含了一大块代码。 GAE日志中的错误消息如下:
<type 'exceptions.NameError'>: name 'self' is not defined
Traceback (most recent call last):
File "/base/data/home/apps/lpflipstud/1.354982193405081399/example.py", line 99, in <module>
class HomeHandler(BaseHandler):
File "/base/data/home/apps/lpflipstud/1.354982193405081399/example.py", line 103, in HomeHandler
logging.info(self.current_user)
这是代码。它是facebook为Google App Engine界面提供的示例代码的略微修改版本,带有facebook应用程序:
class BaseHandler(webapp.RequestHandler):
#Provides access to the active Facebook user in self.current_user.
#The property is lazy-loaded on first access, using the cookie saved
#by the Facebook JavaScript SDK to determine the user ID of the active
#user. See http://developers.facebook.com/docs/authentication/ for
#more information.
@property
def current_user(self):
if not hasattr(self, "_current_user"):
self._current_user = None
cookie = facebook.get_user_from_cookie(
self.request.cookies, FACEBOOK_APP_ID, FACEBOOK_APP_SECRET)
if cookie:
# Store a local instance of the user data so we don't need
# a round-trip to Facebook on every request
user = User.get_by_key_name(cookie["uid"])
if not user:
graph = facebook.GraphAPI(cookie["access_token"])
profile = graph.get_object("me")
id=str(profile["id"])
at = cookie["access_token"]
user = User(key_name=str(profile["id"]),
id=str(profile["id"]),
name=profile["name"],
profile_url=profile["link"],
access_token=cookie["access_token"])
user.put()
elif user.access_token != cookie["access_token"]:
user.access_token = cookie["access_token"]
user.put()
#add list of friend ids
tic = time.clock()
url = "https://graph.facebook.com/"+user.id+"/friends? access_token="+user.access_token
response = urllib2.urlopen(url)
fb_response = response.read()
x = re.findall(r'"\d+"',fb_response)
friend_list = [a.strip('"') for a in x]
user.friends = friend_list
toc = time.clock()
t = toc - tic
user.put()
self._current_user = user
return self._current_user
class HomeHandler(BaseHandler):
def get(self):
path = os.path.join(os.path.dirname(__file__), "homepage.html")
logging.info(self.current_user)
答案 0 :(得分:5)
logging.info(self.current_user)
超出了定义self
的任何范围(并且会在创建类时运行,这可能不是您想要的)。你需要在一个方法中调用它,例如
def get(self):
path = ...
logging.info(self.current_user)