python属性错误:对象没有属性

时间:2014-05-06 03:34:47

标签: python python-2.7

我有一个班级' UserHandler'继承自' RestHandler'但由于某种原因它不能正常继承,因为我不断得到一个' AttributeError:' UserHandler'对象没有属性' sendJson''错误。我正在运行python 2.7顺便说一句。

下面的代码和追溯:

import json
import webapp2

class RestHandler(webapp2.RequestHandler):

  def dispatch(self):
    #time.sleep(1)
    super(RestHandler, self).dispatch()


  def SendJson(self, r):
    self.response.headers['content-type'] = 'text/plain'
    self.response.write(json.dumps(r))

class UserHandler(RestHandler):
  def post(self):
    r = json.loads(self.request.body)
    user = modelUser.InsertUser(r['email'], r['firebaseUid'], r['firebaseId'])
    r = modelUser.AsDict(user)
    self.sendJson(r)

回溯:

Traceback (most recent call last):
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/runtime/wsgi.py", line 266, in Handle
    result = handler(dict(self._environ), self._StartResponse)
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2-2.3/webapp2.py", line 1519, in __call__
    response = self._internal_error(e)
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2-2.3/webapp2.py", line 1511, in __call__
    rv = self.handle_exception(request, response, e)
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2-2.3/webapp2.py", line 1505, in __call__
    rv = self.router.dispatch(request, response)
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2-2.3/webapp2.py", line 1253, in default_dispatcher
    return route.handler_adapter(request, response)
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2-2.3/webapp2.py", line 1077, in __call__
    return handler.dispatch()
  File "main.py", line 25, in dispatch
    super(RestHandler, self).dispatch()
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2-2.3/webapp2.py", line 547, in dispatch
    return self.handle_exception(e, self.app.debug)
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2-2.3/webapp2.py", line 545, in dispatch
    return method(*args, **kwargs)
  File "main.py", line 37, in post
    self.sendJson(r)
AttributeError: 'UserHandler' object has no attribute 'sendJson'

3 个答案:

答案 0 :(得分:4)

代码定义为SendJson(大写)。但是子类调用sendJson。 (小写)

答案 1 :(得分:3)

在最后几行中:

class UserHandler(RestHandler):
  def post(self):
    r = json.loads(self.request.body)
    user = modelUser.InsertUser(r['email'], r['firebaseUid'], r['firebaseId'])
    r = modelUser.AsDict(user)
    self.SendJson(r)
         ^
   Should be capitalized

希望有所帮助:D

答案 2 :(得分:2)

将这些'资本化(或者更好的是,使功能' S'小写)