2
我一直在阅读一些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()',但很难从这里确定意图。 – user2357112