Can "self" crush itself?
Ben Finney
ben+python at benfinney.id.au
Wed Nov 25 00:12:26 EST 2009
n00m <n00m at narod.ru> writes:
> def crush_me(self):
> print 'Will self be crushed?'
> self = None
As with any function, the parameter is bound to a *local* name, in this
case the name ‘self’. Whatever you rebind ‘self’ to inside the function,
the binding is lost once the function exits. None of this affects any
other bindings the same object might retain from outside the function.
It's exactly the same behaviour as this:
>>> def frobnicate(foo):
... print "Entered ‘frobnicate’"
... foo = None
... print "Leaving ‘frobnicate’"
...
>>> bar = "syzygy"
>>> print bar
syzygy
>>> frobnicate(bar)
Entered ‘frobnicate’
Leaving ‘frobnicate’
>>> print bar
syzygy
The only difference with an instance method is how Python determines
what object to bind to the local name ‘self’. That still doesn't change
the fact that it's a local name inside that function.
--
\ “Read not to contradict and confute, nor to believe and take |
`\ for granted … but to weigh and consider.” —Francis Bacon |
_o__) |
Ben Finney
More information about the Python-list
mailing list