Re: Delete a instance passed by reference into a method

Discussion in 'Python' started by Gabriel Genellina, Dec 4, 2007.

  1. En Mon, 03 Dec 2007 19:53:20 -0300, sccs cscs <> escribió:

    > I am very surprising by the Python interpreter behavior : see code
    > I initialize a 'A' and a 'B', and i give a B instance reference to the
    > instance A in 'm1' method. I can modify (IN/OUT mode) the 'i'
    > attribute ( aB.i = 10 ) , BUT I CANNOT DELETE "aB" into the fonction
    > m1 ! the code " del aB " or " aB= None" has no effect. 'aB' still
    > exists when m1() is finsihed! WHY ? How can i do that?


    First, read this <http://effbot.org/zone/python-objects.htm>


    > class A(object):
    > def __init__(self):
    > pass
    > def m1 (self, aB ):
    > aB.i = 10
    > del aB
    > print "no more B"
    > class B(object):
    > def __init__(self,i):
    > self.i = i
    > def __del__(self):
    > print "delete B"
    >
    > aA = A ()
    > aB = B ( i = 6)
    >
    > unA.m1 (aB )
    > print str( aB .i ) #---> Display 10, aB is not destroy !


    The "del" statement does NOT delete the object, it only removes the name
    from the current namespace (and doing so, it decrements the object's
    reference count by one). If that was the last reference to the object, it
    becomes a candidate for being garbage collected later.

    __del__ is not used too much. If you want to ensure resource deallocation,
    use either a try/finally block or a with statement:

    f = open('filename'):
    try
    for line in f:
    do_something_with(line)
    finally
    f.close()

    Another alternative, Python 2.5 and up:

    from __future__ import with_statement

    with open('filename') as f:
    for line in f:
    do_something_with(line)
    # there is an implicit f.close() here

    If you want to use __del__ for another purpose, please describe your use
    case (what you want to achieve, not how you think you should implement it).

    --
    Gabriel Genellina
    Gabriel Genellina, Dec 4, 2007
    #1
    1. Advertising

  2. Gabriel Genellina

    hdante Guest

    On Dec 3, 10:02 pm, "Gabriel Genellina" <>
    wrote:
    > En Mon, 03 Dec 2007 19:53:20 -0300, sccs cscs <> escribió:
    >
    > > I am very surprising by the Python interpreter behavior : see code
    > > I initialize a 'A' and a 'B', and i give a B instance reference to the
    > > instance A in 'm1' method. I can modify (IN/OUT mode) the 'i'
    > > attribute ( aB.i = 10 ) , BUT I CANNOT DELETE "aB" into the fonction
    > > m1 ! the code " del aB " or " aB= None" has no effect. 'aB' still
    > > exists when m1() is finsihed! WHY ? How can i do that?

    >
    > First, read this <http://effbot.org/zone/python-objects.htm>
    >
    >
    >
    > > class A(object):
    > > def __init__(self):
    > > pass
    > > def m1 (self, aB ):
    > > aB.i = 10
    > > del aB
    > > print "no more B"
    > > class B(object):
    > > def __init__(self,i):
    > > self.i = i
    > > def __del__(self):
    > > print "delete B"

    >
    > > aA = A ()
    > > aB = B ( i = 6)

    >
    > > unA.m1 (aB )
    > > print str( aB .i ) #---> Display 10, aB is not destroy !

    >
    > The "del" statement does NOT delete the object, it only removes the name
    > from the current namespace (and doing so, it decrements the object's
    > reference count by one). If that was the last reference to the object, it
    > becomes a candidate for being garbage collected later.
    >
    > __del__ is not used too much. If you want to ensure resource deallocation,
    > use either a try/finally block or a with statement:
    >
    > f = open('filename'):
    > try
    > for line in f:
    > do_something_with(line)
    > finally
    > f.close()
    >
    > Another alternative, Python 2.5 and up:
    >
    > from __future__ import with_statement
    >
    > with open('filename') as f:
    > for line in f:
    > do_something_with(line)
    > # there is an implicit f.close() here
    >
    > If you want to use __del__ for another purpose, please describe your use
    > case (what you want to achieve, not how you think you should implement it)..
    >
    > --
    > Gabriel Genellina


    (note, you don't want to do this, it's a proof of concept)

    import sys

    class A(object):
    def __init__(self):
    pass
    def m1(self, x = None):
    if x == None:
    x = sys._getframe(1).f_locals
    ab = 'aB'
    x[ab].i = 10
    del x[ab]
    print 'No more B'
    class B(object):
    def __init__(self, i):
    self.i = i
    def __del__(self):
    print 'delete B'

    aA = A()
    aB = B(i = 6)
    print str(aB.i)
    aA.m1()
    print str(aB.i)


    Outputs:
    In python:
    6
    delete B
    No more B
    Traceback (most recent call last):
    File "del.py", line 23, in <module>
    print str(aB.i)
    NameError: name 'aB' is not defined

    In jython:
    6
    No more B
    Traceback (innermost last):
    File "del.py", line 23, in ?
    NameError: aB
    hdante, Dec 4, 2007
    #2
    1. Advertising

  3. On Mon, 2007-12-03 at 18:27 -0800, hdante wrote:
    > (note, you don't want to do this, it's a proof of concept)
    >
    > import sys
    >
    > class A(object):
    > def __init__(self):
    > pass
    > def m1(self, x = None):
    > if x == None:
    > x = sys._getframe(1).f_locals
    > ab = 'aB'
    > x[ab].i = 10
    > del x[ab]
    > print 'No more B'
    > class B(object):
    > def __init__(self, i):
    > self.i = i
    > def __del__(self):
    > print 'delete B'
    >
    > aA = A()
    > aB = B(i = 6)
    > print str(aB.i)
    > aA.m1()
    > print str(aB.i)


    That's not much of a proof of anything. It only works because the last
    block happens to only use globals. If you stick it inside a function
    with local names, it'll cease to "work".

    The bottom line is that you can not modify the namespace of the caller
    within a function, unless you only use globals, and I hope I don't have
    to tell you what a fundamentally bad idea that is.

    My question to the OP is, what are you actually trying to accomplish?

    --
    Carsten Haese
    http://informixdb.sourceforge.net
    Carsten Haese, Dec 4, 2007
    #3
  4. Gabriel Genellina

    hdante Guest

    On Dec 4, 11:47 am, Carsten Haese <> wrote:
    > On Mon, 2007-12-03 at 18:27 -0800, hdante wrote:
    > > (note, you don't want to do this, it's a proof of concept)

    >
    > > import sys

    >
    > > class A(object):
    > > def __init__(self):
    > > pass
    > > def m1(self, x = None):
    > > if x == None:
    > > x = sys._getframe(1).f_locals
    > > ab = 'aB'
    > > x[ab].i = 10
    > > del x[ab]
    > > print 'No more B'
    > > class B(object):
    > > def __init__(self, i):
    > > self.i = i
    > > def __del__(self):
    > > print 'delete B'

    >
    > > aA = A()
    > > aB = B(i = 6)
    > > print str(aB.i)
    > > aA.m1()
    > > print str(aB.i)

    >
    > That's not much of a proof of anything. It only works because the last
    > block happens to only use globals. If you stick it inside a function
    > with local names, it'll cease to "work".
    >
    > The bottom line is that you can not modify the namespace of the caller
    > within a function, unless you only use globals, and I hope I don't have


    Namespace modification is used in ORMs, for example. The above
    example works in class definitions. I don't know why it doesn't work
    in functions.

    > to tell you what a fundamentally bad idea that is.
    >
    > My question to the OP is, what are you actually trying to accomplish?
    >
    > --
    > Carsten Haesehttp://informixdb.sourceforge.net
    hdante, Dec 4, 2007
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. dee
    Replies:
    9
    Views:
    505
    Joseph Byrns
    Apr 15, 2005
  2. Stef Mientki
    Replies:
    0
    Views:
    187
    Stef Mientki
    Nov 20, 2008
  3. Stef Mientki
    Replies:
    0
    Views:
    253
    Stef Mientki
    Nov 20, 2008
  4. Raj Singh
    Replies:
    2
    Views:
    196
    Rick DeNatale
    May 29, 2008
  5. Greg Hauptmann
    Replies:
    9
    Views:
    245
    Loren Segal
    Jun 16, 2008
Loading...

Share This Page