Last active
October 25, 2017 13:18
-
-
Save skuro/d247775154d25bced333f543ff08f7e1 to your computer and use it in GitHub Desktop.
Using a property object
This file contains hidden or 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 Book: | |
def __init__(self, title, author): | |
self.title = title | |
self.author = author | |
def getShortDescription(self): | |
"""The getter for the shortDescription property""" | |
return self.title + ' was written by ' + self.author | |
def setShortDescription(self, shortDescription): | |
"""The setter for the shortDescription property""" | |
print("Cannot set a short description!") | |
def delShortDescription(self): | |
"""The deleter for the shortDescription property""" | |
print("Cannot delete a short description!") | |
shortDescription = property(getShortDescription, setShortDescription, delShortDescription, "A short description of the book") | |
b = Book("Das Kapital", "Karl Marx") | |
# using the getter: | |
print(b.shortDescription) # -> Das Kapital was written by Karl Marx | |
# using the setter: | |
b.shortDescription = 42 # -> Cannot set a short description! | |
# using the deleter: | |
del b.shortDescription # -> Cannot delete a short description! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment