Browse Source

example for useful use of property function

master
T. Meissner 10 years ago
parent
commit
427eac2f82
1 changed files with 18 additions and 0 deletions
  1. +18
    -0
      python_3_oop/chapter05/webpage.py

+ 18
- 0
python_3_oop/chapter05/webpage.py View File

@ -0,0 +1,18 @@
from urllib.request import urlopen
import time
class WebPage:
def __init__(self, url):
self.url = url
self._content = None
@property
def content(self):
now = time.time()
if not self._content:
print("Retrieving new page...")
self._content = urlopen(self.url).read()
print("Got page in {} seconds".format(time.time() - now))
return self._content

Loading…
Cancel
Save