Skip to content

Instantly share code, notes, and snippets.

@jalcoding8
Last active August 16, 2021 15:44
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 jalcoding8/b4384f11b89fdc6a7385ea5c3fdc143c to your computer and use it in GitHub Desktop.
Save jalcoding8/b4384f11b89fdc6a7385ea5c3fdc143c to your computer and use it in GitHub Desktop.
OOP/Encapsulation/getters-setters-delete
class Employee:
new_id = 1
def __init__(self, name=None):
self.id = Employee.new_id
Employee.new_id += 1
self._name = name
# Write your code below
def get_name(self):
try:
return self._name
except:
print("No name found")
return "Need to set a name"
#Below is the original code block
#as indicated by instruction 1
#when get_name() called after del_name()
#see line 47
#results in an AttributeError
#so I added the try/except above
"""def get_name(self):
return self._name
"""
def set_name(self, new_name):
self._name = new_name
def del_name(self):
print("name Deleted")
del self._name
e1 = Employee("Maisy")
e2 = Employee()
print(e1.get_name())
e2.set_name("Fluffy")
print(e2.get_name())
e2.del_name()
print(e2.get_name())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment