johnf wrote:
> On Wednesday 12 September 2007 21:27, Paul McNett wrote:
>> [EMAIL PROTECTED] wrote:
>>> Nate,
>>> That's exactly what I came up with. We were just wondering if that was
>>> the only way to do it.
>>>
>>> What's cool about this is I can refer to the object by doing the
>>> following...
>>>
>>> for i in range(1, 10):
>>> exec("o=self.biz%s = dabo.biz.dBizObj()" % i)
>>> o.CurrentSql=....
>>> o.do_something_else
>>>
>>> Thanks
>>> Larry Long
>>>
>>> p.s If I executed the code above, does
>>> o=None
>>> Release the bizobj? I don't think it does. if not, then how can I
>>> release it?
>> Objects in Python are released by the garbage collector when there are
>> no more references to them from anywhere. So whether you use the exec()
>> or setattr() method (but you should use setattr) the bizobjs you create
>> in that loop will stay alive as long as o is in scope (just the single
>> iteration in the for loop) and as long as the object referred to as
>> 'self' is alive, if you never delete the reference from self using e.g.
>> delattr().
>>
>> So your short answer is: no, calling o=None will not release the bizobj
>> (because self has a reference to it).
>>
>> The longer answer requires asking more questions:
>>
>> + If you don't want to keep the bizobj around, why bother attaching it
>> to self? IOW:
>>
>> for i in range(10):
>> o = dabo.biz.dBizobj()
>> o.UserSQL = ...
>>
>> Now the bizobj goes out of scope after the iteration.
>>
>> + You probably do want to keep the bizobj around, so that makes me
>> wonder why you asked about setting o=None to release it, when o is only
>> active inside the for loop.
>>
>> But, here's how to release the bizobj based on what I know about what
>> you are doing:
>>
>> for i in range(1, 10):
>> name_of_biz_to_delete = "biz%s" % i
>> delattr(self, name_of_biz_to_delete)
>>
>> This will work if you haven't saved a reference to that bizobj in any
>> other object.
> I believe the issue revolves around a recursive method Larry is attempting to
> write. The method adds/determines the cost for parent and children records
> and he is concerned that the objects he is recreating will use to much
> memory. Bottom line after obtaining the data (the cost in this case) the
> object is no longer needed.
>
> As I understand it the object will not go out of scope because of the
> recursion.
>
> def start(self):
> ...do something that includes creating an object
> if something:
> call end()
>
> def end(self):
> do something
> call start()
>
> It would be nice to release the object after obtaining the cost.
Ed already noted that setting up a chain of bizobjs just to delete them
smells of incorrectness, however let me just affirm that the object will
go out of scope at the end of the recursive function, *if* you haven't
assigned any of the biz references to anything external, which you
wouldn't need to do if you only needed access to the bizobjs from inside
the recursive function.
--
pkm ~ http://paulmcnett.com
_______________________________________________
Post Messages to: [email protected]
Subscription Maintenance: http://leafe.com/mailman/listinfo/dabo-users
Searchable Archives: http://leafe.com/archives/search/dabo-users
This message: http://leafe.com/archives/byMID/dabo-users/[EMAIL PROTECTED]