Skip to content

Instantly share code, notes, and snippets.

@skuro
Last active October 25, 2017 13:18
Show Gist options
  • Save skuro/d247775154d25bced333f543ff08f7e1 to your computer and use it in GitHub Desktop.
Save skuro/d247775154d25bced333f543ff08f7e1 to your computer and use it in GitHub Desktop.
Using a property object
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