Naive question

Peter Schneider-Kamp petersc at stud.ntnu.no
Tue Jun 20 01:10:55 EDT 2000
Duncan Smith wrote:
> 
> def start():
>     f = Forest()

"f" is variable local to the start() function. When
start exits (after executing its only command) there
is no reference left for f and it is cleaned up.
Even if it was not cleaned up, it would still be local
and thereby unaccessible to you.

> How can I achieve this without creating the Forest instance at the command
> line?  Cheers.  Any help appreciated.

The obvious solution would be:

def start():
    return Forest()

If for some reason you don't want to have the corresponding
f = start() call, you can always leave out these evil
"from x import *" statements and use a variable global to
your module:

f = None

def start():
    global f
    f = Forest()

then you cann access it by
mymodulesname.f after running mymodulesname.start()

Doing this with the "from x import *" statement does not
work as the f imported thereby will still reference None.

If the start method is only to be run once, than you can
put the call to it inside the module:

def start():
    return Forest()

class Forest:
    pass

f = start()

I'd prefer the obvious solution (just use return Forest())
though. Why do you want to do it like that anyway?

what-channel-is-the-timbot-on-ly y'rs
Peter
--
Peter Schneider-Kamp          ++47-7388-7331
Herman Krags veg 51-11        mailto:peter at schneider-kamp.de
N-7050 Trondheim              http://schneider-kamp.de




More information about the Python-list mailing list