Skip to content

Instantly share code, notes, and snippets.

@AnjaneyuluBatta505
Created January 26, 2018 12:55
Show Gist options
  • Save AnjaneyuluBatta505/1c27e497c354c9cc1bf6751edbd36f55 to your computer and use it in GitHub Desktop.
Save AnjaneyuluBatta505/1c27e497c354c9cc1bf6751edbd36f55 to your computer and use it in GitHub Desktop.
suage of python property
class Person(object):
def __init__(self, first_name, last_name):
self.first_name = first_name
self.last_name = last_name
@property
def full_name(self):
return self.first_name + ' ' + self.last_name
@full_name.setter
def full_name(self, value):
first_name, last_name = value.split(' ')
self.first_name = first_name
self.last_name = last_name
@full_name.deleter
def full_name(self):
del self.first_name
del self.last_name
p = Person('Shiva', 'D')
print(p.first_name)
# Output: Shiva
print(p.full_name)
# Output: Shiva D
# Set attributes to class instance using property
p.full_name = 'Surya K'
print(p.first_name)
# Output: Surya
print(p.last_name)
# Output: K
# Delete attribute with "del" statement
del p.full_name
print(p.first_name, p.last_name)
# Output: AttributeError: 'Person' object has no attribute 'first_name'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment