Last active
September 18, 2020 01:17
-
-
Save spinfish/dcf85335fa7fb220d845cf7f61834fd2 to your computer and use it in GitHub Desktop.
A basic example of the Python getter, setter, and deleter methods for properties.
This file contains 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 Person: | |
# Here our __init__ constructor takes a single parameter: `name`. | |
# Since we have not given a default value here, if we tried to instantiate the class without passing | |
# a `name` argument, TypeError would be raised: "__init__() missing 1 required positional argument: 'name'". | |
def __init__(self, name): | |
self._name = name | |
# Here with the @property decorator we are creating a `name` attribute for our Person instance. | |
@property | |
def name(self): | |
return self._name | |
# This method will be called whenever we have `del Person().name` in our code. | |
# If we did not have this method, an AttributeError would be raised: "can't delete attribute". | |
@name.deleter | |
def name(self): | |
print("Deleting my name...") | |
del self._name | |
# This method will be called whenever we access the `name` attribute | |
# of a Person instance, i.e. whenever we have `Person().name` in our code. | |
@name.getter | |
def name(self): | |
try: | |
return self._name | |
# We have a try/except here in case our name has been deleted via the `deleter` method, | |
# in which case it would raise AttributeError: "'Person' object has no attribute '_name'". | |
except AttributeError: | |
return "My name has been deleted!" | |
# This method will be called whenever we want to reassign a new `name` attribute to our | |
# Person instance. Without this method, if we tried to do this (Person().name = '...') | |
# it would raise an AttributeError: "can't set attribute". | |
# The method we have created here takes a single parameter, `new_name`. | |
@name.setter | |
def name(self, new_name): | |
print("Renaming myself...") | |
self._name = new_name | |
John = Person('John') | |
print(f'My name is: {John.name}') | |
del John.name | |
# This will print "My name has been deleted!" because our `deleter` method was called. | |
print(John.name) | |
# This calls the `setter` method we created | |
John.name = 'Peter' | |
# This will print 'My new name is: Peter' because we have reassigned the `name` attribute. | |
print(f'My new name is: {John.name}') |
This file contains 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 Person: | |
def __init__(self, name): | |
self._name = name | |
@property | |
def name(self): | |
return self._name | |
@name.deleter | |
def name(self): | |
print("Deleting my name...") | |
del self._name | |
@name.getter | |
def name(self): | |
try: | |
return self._name | |
except AttributeError: | |
return "My name has been deleted!" | |
@name.setter | |
def name(self, new_name): | |
print("Renaming myself...") | |
self._name = new_name | |
John = Person('John') | |
print(f'My name is: {John.name}') | |
del John.name | |
print(John.name) | |
John.name = 'Peter' | |
print(f'My new name is: {John.name}') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment