2

Let's say I want to implement the following functionality.

I have a class Employee, which has 3 variables viz, name age and salary. I will override the __add__ and use it to create a single object that will be a combination of the two objects used. For example, if I have A and B, and if I do A + B, I want their information to be combined. I am able to do it. But now I want to delete one of the objects, let's say B, so that there won't exist that B Employee in the system. I tried using del, but I can still print its values after deletion.

class Employee:
    __slots__ = ['__name', '__age', '__salary']

    def __init__(self, name, age, salary):
        self.__name = name
        self.__age = age
        self.__salary = salary

    def display(self):
        print(f"\nName: {self.__name}")
        print(f"Age: {self.__age}")
        print(f"Salary: {self.__salary}")
    
    def __add__(self, other):
        finalName = f"{self.__name} & {other.__name}"
        finalAge = (self.__age + other.__age) / 2
        finalSalary = self.__salary + other.__salary
        del other
        return Employee(finalName, finalAge, finalSalary)

    
a = Employee("A", 22, 10000)
b = Employee("B", 25, 15000)

a.display()
b.display()

# Prints as expected
Name: A
Age: 22
Salary: 10000

Name: B
Age: 25
Salary: 15000

a = a + b

a.display()
# Additon operations takes place successfully and it prints the following

Name: A & B
Age: 23.5
Salary: 25000

b.display()

# Actual Behaviour
Name: B
Age: 25
Salary: 15000

# Expected Behaviour
Some error saying B does not exist or not defined
SuperStormer
  • 4,997
  • 5
  • 25
  • 35
  • [Relevant](https://stackoverflow.com/questions/21053380/what-does-del-do-exactly) – SuperStormer Apr 04 '21 at 18:08
  • what happen if you change with ? (sorry to bother dont have my python machine with me ) – pippo1980 Apr 04 '21 at 18:13
  • You cannot manually delete objects in Python. This kind of operation should be a "logical" rather than "physical" operation, removing the object from appropriate data structures instead of trying to directly make it stop existing. – user2357112 Apr 04 '21 at 18:16
  • 1
    (This is, of course, ignoring the fact that adding two employees together makes no sense.) – user2357112 Apr 04 '21 at 18:16
  • You can't delete the `B` object because the `b` variable still references it. When you `del` something, it only decreases its reference count by one. When it reaches zero, the interpreter _may_ actually delete it, but that's not guarantee. You can add a `__delete__()` special method to your class the prints something whenever it's called and prove this to yourself. – martineau Apr 04 '21 at 18:36
  • @pippo1980 other = None doesn't work – Shreyas Vedpathak Apr 06 '21 at 10:29
  • tried it, only thing I was able to do is set name , age salary to none see https://stackoverflow.com/questions/53729046/delete-class-instance-from-memory/53729141 here https://stackoverflow.com/questions/16686788/python-how-to-kill-a-class-instance-object and so on If you fina a way please tell us I bookmarked your post – pippo1980 Apr 06 '21 at 13:54
  • @pippo1980 will do ! – Shreyas Vedpathak Apr 07 '21 at 14:05
  • i do not understand the requirement to delete B.. coz u can do `c = a + b` , and if only b is deleted, a is still left bound.. – a_n Jun 23 '22 at 14:16

0 Answers0