Python Forum
can someone explain this __del__ behaviour?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
can someone explain this __del__ behaviour?
#1
I came across something interesting that I can't explain. Perhaps someone here can. When the following code is run with the lambda commented out it produces the output I expect to see.

    class Exp:
        def __init__(self):
            print('__init__')
            self.b = 1
            #self.a = lambda: self.b

        def __del__(self):
            print('__del__')

    obj = Exp()
    obj = None
Output

    __init__
    __del__
But with the lambda uncommented

    class Exp:
        def __init__(self):
            print('__init__')
            self.b = 1
            self.a = lambda: self.b

        def __del__(self):
            print('__del__')

    obj = Exp()
    obj = None
I do not see that __del__ is executed.

    __init__
As I understand it, __del__ executes when the reference count for an object reaches zero so I can only assume that somehow the lambda line is doing something odd. But if I modify the lambda to

    class Exp:
        def __init__(self):
            print('__init__')
            self.b = 1
            self.a = lambda: 10

        def __del__(self):
            print('__del__')

    obj = Exp()
    obj = None

I get

    __init__
    __del__
I'm not a fan of lambdas, but typically a lambda specifies a parameter and something that operates on that parameter like

    self.a = lambda x: x**2


so perhaps specifying self.b in the lambda expression is causing a problem with the reference counter.
Reply


Messages In This Thread
can someone explain this __del__ behaviour? - by rjdegraff42 - Apr-12-2023, 02:37 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  logger behaviour setdetnet 1 946 Apr-15-2023, 05:20 AM
Last Post: Gribouillis
  Asyncio weird behaviour vugz 2 1,354 Apr-09-2023, 01:48 AM
Last Post: vugz
  Weird behaviour using if statement in python 3.10.8 mikepy 23 3,943 Jan-18-2023, 04:51 PM
Last Post: mikepy
  Generator behaviour bla123bla 2 1,166 Jul-26-2022, 07:30 PM
Last Post: bla123bla
  Inconsistent behaviour in output - web scraping Steve 6 2,691 Sep-20-2021, 01:54 AM
Last Post: Larz60+
  Adding to the dictionary inside the for-loop - weird behaviour InputOutput007 5 2,845 Jan-21-2021, 02:21 PM
Last Post: InputOutput007
  Behaviour of 2D array SimonB 6 2,957 Jan-21-2021, 01:29 PM
Last Post: SimonB
  strange behaviour- plotting nathan_Blanc_Haifa 0 1,536 Dec-27-2020, 01:37 PM
Last Post: nathan_Blanc_Haifa
  OOP behaviour problem JohnB 3 2,483 Aug-18-2020, 07:51 PM
Last Post: JohnB
  understanding basic loop behaviour vinci 5 3,009 Feb-11-2020, 09:53 PM
Last Post: vinci

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020