[Tutor] Is "var = None" in Python equivalent to "Set var
wescpy at gmail.com
Sun Jun 29 22:59:06 CEST 2008
> >>the reason why i ask is because it's not standard practice i see
> >>people doing this with Python,
i've been programming in Python continously/full-time for the past 11
yrs at 4+ companies, and i'll stand by my statement... i just don't
see it in the code, nor do i do this myself. i cannot tell you
*exactly* why, other than most of the time, people let objects go
out-of-scope and are reclaimed "naturally" or more rarely, i see
people calling del on an object to remove it from the namespace
explicitly. now, what i *do* see a lot is where a variable is
originally initialized to None.
> Since when is setting anything to None an outdated practice?
not sure what you mean here, but i never said anything like this in my
reply, nor was there any reference to anything being outdated.
> Does this mean checking an object to None is also outdated?
again, i'm not sure what you mean here as my reply never inferred
this. although i am curious... how *do* you check an object to None?
(this is a completely tangential thread, but i'm curious if the
beginners -- not the experts -- are familiar with the difference
between "if obj == None" vs. "if obj is None".)
> Setting it to None immediately puts the object in a state where the user
> knows they need to re-initialize it before using it again.
there is no more "object" since you replaced it with None. i'll assume
you mean the variable. the only difference between this and using del
is that you leave the name in the namespace (where it holds a
reference to None).
> del will only reclaim that var at a later time, when the gc kicks in.
again, you said "var" but i'm assuming you mean object. the var is
removed from the namespace and the object's reference count
decremented. the gc won't claim if there are additional aliases or
references to the object.
> Of course, if that object won't be used again in the same scope, does it
> really matter ?
then just let it go out of scope. there's no need to set it to
anything else if it won't be used again... you're just wasting CPU
cycles and memory access then.
More information about the Tutor