From 0a3d7b8cd52952baae84b01b84e08a3f05799d96 Mon Sep 17 00:00:00 2001 From: tmeissner Date: Sun, 21 Sep 2014 15:38:23 +0200 Subject: [PATCH] more examples of chapter 05 --- python_3_oop/chapter05/color.py | 15 +++++++++++++ python_3_oop/chapter05/silly.py | 37 +++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+) create mode 100644 python_3_oop/chapter05/color.py create mode 100644 python_3_oop/chapter05/silly.py diff --git a/python_3_oop/chapter05/color.py b/python_3_oop/chapter05/color.py new file mode 100644 index 0000000..11e6be7 --- /dev/null +++ b/python_3_oop/chapter05/color.py @@ -0,0 +1,15 @@ +class Color: + + def __init__(self, rgb_value, name): + self.rgb_value = rgb_value + self._name = name + + def _set_name(self, name): + if not name: + raise Exception("Invalid name") + self._name = name + + def _get_name(self): + return self._name + + name = property(_get_name, _set_name) diff --git a/python_3_oop/chapter05/silly.py b/python_3_oop/chapter05/silly.py new file mode 100644 index 0000000..364c063 --- /dev/null +++ b/python_3_oop/chapter05/silly.py @@ -0,0 +1,37 @@ +#class Silly: +# +# def _get_silly(self): +# print("You're getting silly") +# return self._silly +# +# def _set_silly(self, value): +# print("You're making silly {}".format(value)) +# self._silly = value +# +# def _del_silly(self): +# print("Whoa, you're killing silly!") +# del self._silly +# +# silly = property(_get_silly, _set_silly, _del_silly, +# "This is a silly property") + + +# same as before but with decorators: + +class Silly: + + @property + def silly(self): + "This is a silly property" + print("You're getting silly") + return self._silly + + @silly.setter + def silly(self, value): + print("You're making silly {}".format(value)) + self._silly = value + + @silly.deleter + def silly(self): + print("Whoa, you're killing silly!") + del self._silly