Reputation: 139
This is my code for reference:
class Manager(Employee):
def __init__(self, first, last, position, salary):
super().__init__(first, last, position)
self.salary = salary
def getinfo(self):
return self.first, self.last, self.position, self.salary
def give_raise(self, employee):
if type(employee) == type(Teller_1):
employee.hourly = int(employee.hourly * 1.10)
else:
employee.salary = int(employee.salary * 1.10)
def fire_employee(self, employee):
if type(employee.first) == type(Branch_manager) or type(employee) == type(Bank_executive):
print ("you cannot fire someone in an equal or higher position")
else:
print ("you have successfully fired " + str(employee.first) + " " + str(employee.last))
del employee
print (Analyst_1)
Analyst_1 = SalaryEmployee('Bob', 'Dun', 'Account Analyst', 40000)
When I put in this code:
Branch_manager.fire_employee(Analyst_1)
I want it to remove the object "Analyst_1". However, all it is doing is removing the reference variable "employee" and not the object itself.
How can I remove the actual object itself and not just the reference variable?
Upvotes: 0
Views: 1174
Reputation: 60957
You can delete a name using the del
keyword or the delattr
built-in function, but you can't delete an object. (Objects don't get deleted, they get garbage collected when no names refer to them anymore.)
If your method looks like:
def fire_employee(employee):
...
and you call it like
fire_employee(Analyst_1)
then the object that the name Analyst_1
refers to will be the same object that the name employee
refers to. So even if you delete one of those names, the object itself will still exist as long as the other name still references it.
You can delete the name from the global namespace, if you know what namespace it is and what name it is:
def fire_employee(employee_name):
module_object = sys.modules[__name__]
delattr(module_object, employee_name)
which you would call like this instead:
# Note that we pass a *string*, "Analyst_1", and not the *object*.
fire_employee('Analyst_1')
But if you're going to go that route, you'd be better off keeping the employee name to object mapping in its own data structure, likely a dict
object as was already suggested.
Upvotes: 1