8000 Merge pull request #582 from realpython/python-property · realpython/materials@66d67b3 · GitHub
[go: up one dir, main page]

Skip to content

Commit 66d67b3

Browse files
authored
Merge pull request #582 from realpython/python-property
Sample code for the article on properties
2 parents 16be0cf + f0b6542 commit 66d67b3

22 files changed

+382
-0
lines changed

python-property/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1< 8000 span class="diff-text-marker">+
# Python's property(): Add Managed Attributes to Your Classes
2+
3+
This folder provides the code examples for the Real Python tutorial [Python's property(): Add Managed Attributes to Your Classes](https://realpython.com/python-property/).

python-property/circle_v1.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Circle:
2+
def __init__(self, radius):
3+
self._radius = radius
4+
5+
def _get_radius(self):
6+
print("Get radius")
7+
return self._radius
8+
9+
def _set_radius(self, value):
10+
print("Set radius")
11+
self._radius = value
12+
13+
def _del_radius(self):
14+
print("Delete radius")
15+
del self._radius
16+
17+
radius = property(
18+
fget=_get_radius,
19+
fset=_set_radius,
20+
fdel=_del_radius,
21+
doc="The radius property.",
22+
)

python-property/circle_v2.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Circle:
2+
def __init__(self, radius):
3+
self._radius = radius
4+
5+
@property
6+
def radius(self):
7+
"""The radius property."""
8+
print("Get radius")
9+
return self._radius
10+
11+
@radius.setter
12+
def radius(self, value):
13+
print("Set radius")
14+
self._radius = value
15+
16+
@radius.deleter
17+
def radius(self):
18+
print("Delete radius")
19+
del self._radius

python-property/circle_v3.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Circle:
2+
def __init__(self, radius):
3+
self.radius = radius
4+
5+
@property
6+
def radius(self):
7+
return self._radius
8+
9+
@radius.setter
10+
def radius(self, value):
11+
self._radius = float(value)
12+
13+
@property
14+
def diameter(self):
15+
return self.radius * 2
16+
17+
@diameter.setter
18+
def diameter(self, value):
19+
self.radius = value / 2

python-property/circle_v4.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
from time import sleep
2+
3+
4+
class Circle:
5+
def __init__(self, radius):
6+
self.radius = radius
7+
self._diameter = None
8+
9+
@property
10+
def diameter(self):
11+
if self._diameter is None:
12+
sleep(0.5) # Simulate a costly computation
13+
self._diameter = self.radius * 2
14+
return self._diameter

python-property/circle_v5.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
from time import sleep
2+
3+
4+
class Circle:
5+
def __init__(self, radius):
6+
self.radius = radius
7+
8+
@property
9+
def radius(self):
10+
return self._radius
11+
12+
@radius.setter
13+
def radius(self, value):
14+
self._diameter = None
15+
self._radius = value
16+
17+
@property
18+
def diameter(self):
19+
if self._diameter is None:
20+
sleep(0.5) # Simulate a costly computation
21+
self._diameter = self._radius * 2
22+
return self._diameter

python-property/circle_v6.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from functools import cached_property
2+
from time import sleep
3+
4+
5+
class Circle:
6+
def __init__(self, radius):
7+
self.radius = radius
8+
9+
@cached_property
10+
def diameter(self):
11+
sleep(0.5) # Simulate a costly computation
12+
return self.radius * 2

python-property/circle_v7.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
from functools import cache
2+
from time import sleep
3+
4+
5+
class Circle:
6+
def __init__(self, radius):
7+
self.radius = radius
8+
9+
@property
10+
@cache
11+
def diameter(self):
12+
sleep(0.5) # Simulate a costly computation
13+
return self.radius * 2

python-property/circle_v8.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import logging
2+
3+
logging.basicConfig(
4+
format="%(asctime)s: %(message)s",
5+
level=logging.INFO,
6+
datefmt="%H:%M:%S",
7+
)
8+
9+
10+
class Circle:
11+
def __init__(self, radius):
12+
self._msg = '"radius" was %s. Current value: %s'
13+
self.radius = radius
14+
15+
@property
16+
def radius(self):
17+
logging.info(self._msg % ("accessed", str(self._radius)))
18+
return self._radius
19+
20+
@radius.setter
21+
def radius(self, value):
22+
try:
23+
self._radius = float(value)
24+
logging.info(self._msg % ("mutated", str(self._radius)))
25+
except ValueError:
26+
logging.info('validation error while mutating "radius"')

python-property/currency_v1.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class Currency:
2+
def __init__(self, units, cents):
3+
self.units = units
4+
self.cents = cents
5+
6+
# Currency implementation...

0 commit comments

Comments
 (0)
0