Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] Python __del__() Magic Method

#1
Python __del__() Magic Method

Python’s magic method __del__() is called the finalizer method or, wrongly, the destructor method — the latter being wrong because it doesn’t actually destroy the object. Python calls __del__() upon deletion of a given instance. For example, the expression del my_obj will eventually initiate my_obj.__del__().

We call this a “Dunder Method” for Double Underscore Method” (also called “magic method”). To get a list of all dunder methods with explanation, check out our dunder cheat sheet article on this blog.

💡 Note: the expression del my_obj actually decrements the reference count for my_obj. It doesn’t directly call my_obj.__del__()because this method is only called when the reference count reaches zero.

Syntax and Example


object.__del__(self)

Let’s have a look at an example:

class MyClass: def __del__(self): print('hello world') my_obj = MyClass()
del my_obj
# hello world

References:



https://www.sickgaming.net/blog/2022/01/...ic-method/
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

[-]
Discord

[-]
Active Threads
(Indie Deal) Fancy Worm Bundle, Capcom &...
Last Post: xSicKxBot
Today 01:31 PM
» Replies: 0
» Views: 2
(Free Game Key) DAEMON X MACHINA - Free ...
Last Post: xSicKxBot
Today 01:31 PM
» Replies: 0
» Views: 1
Mobile - Genshin Impact Lantern Rite 202...
Last Post: xSicKxBot
Today 01:31 PM
» Replies: 0
» Views: 1
AppleInsider - Master and Dynamic MW07 P...
Last Post: xSicKxBot
Today 01:31 PM
» Replies: 0
» Views: 1
Xbox Wire - Join Rocket League’s Celebra...
Last Post: xSicKxBot
Today 01:30 PM
» Replies: 0
» Views: 1
News - Surge XT Synthesizer Release Vers...
Last Post: xSicKxBot
Today 01:30 PM
» Replies: 0
» Views: 2
News - GTA Online Weekly Update: Biker B...
Last Post: xSicKxBot
Today 01:30 PM
» Replies: 0
» Views: 1
(Indie Deal) Handy Games, Offworld, Citi...
Last Post: xSicKxBot
Yesterday 10:43 AM
» Replies: 0
» Views: 58
News - Pokemon-With-Guns Game Palworld C...
Last Post: xSicKxBot
Yesterday 10:42 AM
» Replies: 0
» Views: 13
(Xbox 360) Mod Forza Horizon 2 with USB
Last Post: tekashisama
Yesterday 02:45 AM
» Replies: 11
» Views: 10542

[-]
Twitter



Discord Server © SickGaming.net 2012-2021