如果您只想记录特定的私有方法,而不是全部,您可以automethod
为每个方法使用指令,而不是:private-members:
.
我经常使用与普通公共方法同名的前导下划线方法,它具有函数的实际实现。然后,公共方法对输入参数进行各种健全性检查。下划线方法跳过它们,因此可以调用它们以提高效率,但类型安全性较低。
例如(对@cmcginty 窃取他们的示例表示歉意)
class SmokeMonster(object):
"""
A large smoke monster that protects the island.
"""
def __init__(self, speed, initial_position):
"""
:param speed: Velocity in MPH of the smoke monster
:param inital_position: Position of the smoke monster
"""
self.speed = speed
self.position = initial_position
def _evaporate(self):
"""
Removes the smoke monster from reality. Not to be called by client.
"""
pass
def fly_to(self, position):
"""
Have the monster fly to the specified position.
:param position: Desired location for the monster to fly to.
"""
if not position.is_valid():
raise ValueError("Invalid position: " + str(position))
if not self.can_fly():
raise RuntimeError("Smoke monster is not currently able to fly.")
self._fly_to(position)
def _fly_to(self, position):
"""Equivalent to :meth:`SmokeMonster.fly_to`, but without the safety checks.
Not normally recommended for end users, but available if you need to
improve efficiency of the `fly_to` call and you already know it is safe
to call.
"""
self.position = position
然后记录_fly_to
,但不是_evaporate
,你可以这样做:
.. autoclass:: SmokeMonster
:members:
.. automethod:: SmokeMonster._fly_to