1
我想了解某种OAuth2/web2py集成,但python类模型中的一些怪癖让我难以掌握。具体而言,我有这样的web2py控制器:派生类不调用初始化
def google():
if 'state' in request.vars and request.vars.state == 'google':
session.state = request.vars.state
person = Person("google")
print person.render()
return person.render()
因此,我们在这里使用Person
类。实现是这样的:
class Person(Base):
没有__init__
是存在于Person
类。该Base
类有一个__init__
功能:
class Base(object):
def __init__(
self,
hooks=[],
theme="%(name)s/",
view="app/generic",
meta=None,
context=None
):
from gluon.storage import Storage
self.meta = meta or Storage()
self.context = context or Storage()
self.context.alerts = []
self.context.content_types = []
self.context.categories = []
self.context.menus = []
self.context.internalpages = []
self.theme = theme
self.view = view
# hooks call
self.start()
self.build()
self.pre_render()
# aditional hooks
if not isinstance(hooks, list):
hooks = [hooks]
for hook in hooks:
self.__getattribute__(hook)()
所以我的问题是:如果Person
没有显式调用Base.__init__
,将它在所有被称为?
或者,为了使它更通用:如果派生类没有__init__
函数,将会调用基类__init__
函数吗?如果派生类具有__init__
函数,但没有明确调用基类__init__
函数?
好的。所以'__init__'只是一个正常的函数。正常的重载规则适用。关于它的唯一特别的事情是它会在实例化一个对象时被调用,对吗? – dangonfast 2012-07-25 12:50:31
@gonvaled正确。 – 2012-07-25 15:54:51