Created
July 18, 2017 00:20
-
-
Save EFulmer/58291f50a432f45d6c3e613258fea9a1 to your computer and use it in GitHub Desktop.
property examples
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Ramone(object): | |
def __init__(self, name, instrument): | |
self.name = name | |
self.instrument = instrument | |
@property | |
def countdown(self): | |
if not hasattr(self, '_countdown'): | |
self._countdown = 1 | |
return self._countdown | |
@countdown.setter | |
def countdown(self, new_countdown): | |
if new_countdown not in (1, 2, 3, 4): | |
raise ValueError("ONE TWO THREE FOUR!") | |
self._countdown = new_countdown | |
# alternate definition, without using decorator syntax | |
class Ramone(object): | |
def __init__(self, name, instrument): | |
self.name = name | |
self.instrument = instrument | |
def countdown_getter(self): | |
if not hasattr(self, '_countdown'): | |
self._countdown = 1 | |
return self._countdown | |
def countdown_setter(self, new_countdown): | |
if new_countdown not in (1, 2, 3, 4): | |
raise ValueError("ONE TWO THREE FOUR!") | |
self._countdown = new_countdown | |
countdown = property(countdown_getter, countdown_setter) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment