Napisz docstring na geterze, ponieważ 1) to pokazuje help(MyClass)
, a 2) to także, jak to się robi w Python docs -- see the x.setter example.
Odnośnie 1):
class C(object):
@property
def x(self):
"""Get x"""
return getattr(self, '_x', 42)
@x.setter
def x(self, value):
"""Set x"""
self._x = value
A potem:
>>> c = C()
>>> help(c)
Help on C in module __main__ object:
class C(__builtin__.object)
| Data descriptors defined here:
|
| __dict__
| dictionary for instance variables (if defined)
|
| __weakref__
| list of weak references to the object (if defined)
|
| x
| Get x
>>>
Zauważ, że docstring ustawiająca za "zbiór X" jest ignorowany.
Powinieneś napisać docstring do całej właściwości (getter i setter) na funkcji gettera, aby była widoczna. Przykładem dobrej docstring nieruchomości może być:
class Serial(object):
@property
def baudrate(self):
"""Get or set the current baudrate. Setting the baudrate to a new value
will reconfigure the serial port automatically.
"""
return self._baudrate
@baudrate.setter
def baudrate(self, value):
if self._baudrate != value:
self._baudrate = value
self._reconfigure_port()
Nice, przykład tutaj w dokumentacji Pythona. –
ustawiacz musi być udokumentowany w pobierającym –