【发布时间】:2017-09-01 16:51:00
【问题描述】:
我有一个 Python Web 应用程序,我想定义一个通用类或函数来处理网页,并从更具体的类中调用它以用于特定的页面实例。
错误:
元类冲突:派生类的元类必须是其所有基类的元类的(非严格)子类
我检查了有关该错误消息的所有 StackOverflow 问题和答案,但不理解给出的解释(我发现了一篇关于元类的文章,但我不理解 - OOP 不是我真正的东西)。我还查看了有关从另一个类调用类方法的所有 StackOverflow 问题和答案(也没有理解它们)。
代码:
import webapp2
from datetime import datetime, timedelta
from google.appengine.ext import ndb
from google.appengine.api import users
import admin_templates
import authenticate
class myPage(webapp2.RequestHandler):
def get(pageInstance):
pageInstance.outputHtml()
def outputHtml(pageInstance,pageTitle,pageContent):
adminLink = authenticate.get_adminlink()
authMessage = authenticate.get_authmessage()
adminNav = authenticate.get_adminnav()
pageInstance.response.headers['Content-Type'] = 'text/html'
html = admin_templates.base
html = html.replace('#title#', pageTitle)
html = html.replace('#authmessage#', authMessage)
html = html.replace('#adminlink#', adminLink)
html = html.replace('#adminnav#', adminNav)
html = html.replace('#content#', pageContent)
pageInstance.response.out.write(html)
pageInstance.response.out.write(admin_templates.footer)
class AuditorsPage(webapp2.RequestHandler, myPage.outputHtml):
def get(self):
self.output_auditors()
def output_auditors(self):
self.outputHtml(admin_templates.auditors_title, admin_templates.auditors_content)
如何从AuditorsPage 类调用myPage 类中的outputHtml 方法而不出现元类错误?
【问题讨论】:
-
为什么
AuditorsPage试图从myPage的方法 继承? -
无特殊原因!只是我无能!
-
感谢@bruntime 的编辑,这是一个改进