1

Me and a friend are coding a game using pygame in python, I was wondering if there was a way to override the del keyword in python, similar to how you can override operators (such as += with __iadd__, for example).

I have a kill() function for my sprite but we wondered if we could override the del statement to do something similar (just because it would be a really cool use of sytax)

I've tried the __del__ and __delete__ functions but either I do not understand what these methods are for or they are not run when del is run.

Is there any way to provide a custom method to be run when del is executed?

ex.

class Example:
    def some_magical_function(self) -> None:
        # Other code to delete the object
        print("Object deleted!")

test = Example()
del test
>> Object deleted!

Thank you! PS. My first post so sorry if there are any formatting errors or anything :P

0x5DA
  • 77
  • 1
  • 5

0 Answers0