Skip to content

Instantly share code, notes, and snippets.

@ychennay
Created March 21, 2020 04:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ychennay/4f620ca208c498c243d03deafa96222e to your computer and use it in GitHub Desktop.
Save ychennay/4f620ca208c498c243d03deafa96222e to your computer and use it in GitHub Desktop.
properties
# both classes below implement Python properties using data descriptors
class StudentWithInlineProps:
def __init__(self, name="Default Name"):
self._name = name
# getting the values
def get_name(self):
print('Getting value')
return self._name
# setting the values
def set_name(self, value):
self._name = value
name = property(get_name, set_name)
class StudentWithDecoratorProps:
def __init__(self, name="Default Name"):
self._name = name
@property
def name(self):
return self._name
@name.setter
def name(self, name):
self._name = name
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment