Skip to content

Instantly share code, notes, and snippets.

@sommersoft
Last active August 27, 2020 16:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sommersoft/2334fc628cd2897a72f1672aad8f2cd1 to your computer and use it in GitHub Desktop.
Save sommersoft/2334fc628cd2897a72f1672aad8f2cd1 to your computer and use it in GitHub Desktop.
Overriding Methods With Composition
Press any key to enter the REPL. Use CTRL-D to reload.
Adafruit CircuitPython ce2754d50 on 2020-06-21; Adafruit ItsyBitsy M4 Express with samd51g19
>>> import board
>>> from frequencyio import FrequencyIn
>>> class NewFreqIn:
... def __init__(self, pin):
... self._freqin = FrequencyIn(pin) # composed base class as an attribute of NewFreqIn
...
... @property
... def capture_period(self):
... return "capture_period is: {}".format(self._freqin.capture_period)
...
... @capture_period.setter
... def capture_period(self, value):
... print("setting capture_period to:", value)
... self._freqin.capture_period = value
...
>>> foo = NewFreqIn(board.D2)
# Accessing FrequencyIn.capture_period via the overridden methods in NewFreqIn
>>> foo.capture_period
'capture_period is: 10'
>>> foo.capture_period = 50
setting capture_period to: 50
>>> foo.capture_period
'capture_period is: 50'
# Accessing FrequencyIn.capture_period directly via the NewFreqIn._freqin attribute
>>> foo._freqin.capture_period
10
>>> foo._freqin.capture_period = 50
>>> foo._freqin.capture_period
50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment