【发布时间】:2022-01-23 07:11:32
【问题描述】:
所以我正在查看一些旧的 python 2 代码,我看到了这个函数
def manage_addMapSamlPlugin(self, id, title='', delegate_path='', REQUEST=None):
""" Factory method to instantiate a MapSamlPlugin """
# Make sure we really are working in our container (the
# PluggableAuthService object)
self = self.this()
# Instantiate the adapter object
lmp = MapSamlPlugin(id, title=title, delegate_path=delegate_path )
self._setObject(id, lmp)
if REQUEST is not None:
REQUEST.RESPONSE.redirect('%s/manage_main' % self.absolute_url())
现在这个函数在一个类之外,代码编译并且没有给出任何错误。我的理解是,在这种情况下, self 关键字只是传入的任何内容,但是 self.this() 和 self._setObject(id, lmp) 不应该是正确的?编译器不应该抛出错误吗?该代码在 ssh 服务器的终端上运行,我不知道它使用什么编译器。 在文件的末尾,这是函数被调用的地方。
def initialize(context):
registerMultiPlugin(MapSamlPlugin.meta_type)
context.registerClass(
MapSamlPlugin,
constructors=(manage_addMapSamlPluginForm, manage_addMapSamlPlugin),
permission=ManageUsers,
icon=os.path.join(mgr_dir, "saml_icon.png"),
visibility=None,
)
这也是一个独立的函数“上下文”不是从任何导入或类派生的。
【问题讨论】:
-
为什么会报错?
self不是关键字,甚至不是内置名称。这只是一个约定。但它没有任何特殊意义。您还可以在类中将self替换为baz... -
self不是关键字。self是传统上赋予方法的第一个位置参数的名称,当在实例上调用方法时将隐式传递给实例。
标签: python python-2.x