Delete a instance passed by reference into a method

hdante hdante at gmail.com
Tue Dec 4 18:04:37 EST 2007


On Dec 4, 11:47 am, Carsten Haese <cars... at uniqsys.com> 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




More information about the Python-list mailing list