python - Calling a class method from another class causes metaclass conflict -
i have python web app , want define general class or function processing web pages , call more specific class specific page instances.
error:
metaclass conflict: metaclass of derived class must (non-strict) subclass of metaclasses of bases
i have checked stackoverflow questions & answers error message , did not understand explanations given (and found article metaclasses didn't understand - oop not thing). looked @ stackoverflow questions & answers calling class method class (and didn't understand them either).
code:
import webapp2 datetime import datetime, timedelta google.appengine.ext import ndb 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) how call outputhtml method in mypage class auditorspage class without getting metaclass error?
the error comes fact subclassing class method in general class, can't done. actually, code wrong, , there @ least 2 ways can fix it:
1) inherit mypage instead: in case, won't work , bring mro error because methods not part of instance of class, not linked object using self first parameter.
this fix code in case:
from google.appengine.ext import ndb google.appengine.api import users import admin_templates import authenticate class mypage(webapp2.requesthandler): def get(self, pageinstance): pageinstance.outputhtml() def outputhtml(self, 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(mypage): def get(self): self.output_auditors() 2) 2 methods in mypage class normal methods no class, , call them directly class. actually, need create outputhtml() method, , call directly (without self.) method in requesthandler subclass.
Comments
Post a Comment