Skip to content

Instantly share code, notes, and snippets.

@dmiro
Last active August 29, 2015 14:01
Show Gist options
  • Select an option

  • Save dmiro/57b4dae046466fba1c6b to your computer and use it in GitHub Desktop.

Select an option

Save dmiro/57b4dae046466fba1c6b to your computer and use it in GitHub Desktop.
"""
CHEAT 1: a class with person property using @property decorator
There are two ways to specify a property:
(1) property class
(2) @property decorator
https://docs.python.org/2/library/functions.html#property
"""
class Person(object): # Python > 2.6 you need class clause Person(object)
def __init__(self):
self._name = ""
@property
def name(self):
return self._name
@name.setter
def name(self, value):
self._name = value
@name.deleter
def name(self):
del(self._name)
# Test
person = Person()
print person.name
person.name = "David"
print person.name
del(person.name)
print person.name
# Result:
"""
David
Traceback (most recent call last):
...
AttributeError: 'Person' object has no attribute '_name'
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment