【发布时间】:2017-04-12 17:24:45
【问题描述】:
我一直在阅读一些 Google App Engine SDK 源代码,我注意到 Google 经常编写一个私有类方法(在方法名称前加上 _),但在完成方法代码块之后,他们创建一个同名的公共变量,并将私有方法分配给该变量。
他们为什么这样做?
示例代码:
@classmethod
@utils.positional(3)
def _get_by_id(cls, id, parent=None, **ctx_options):
"""Returns an instance of Model class by ID.
This is really just a shorthand for Key(cls, id, ...).get().
Args:
id: A string or integer key ID.
parent: Optional parent key of the model to get.
namespace: Optional namespace.
app: Optional app ID.
**ctx_options: Context options.
Returns:
A model instance or None if not found.
"""
return cls._get_by_id_async(id, parent=parent, **ctx_options).get_result()
get_by_id = _get_by_id
【问题讨论】:
-
当
self.whatever()被覆盖时,可能仍然可以执行self._whatever(),但很难从这里确定意图。
标签: python class decorator python-decorators