Re: self

Tim Peters (
Thu, 05 May 94 16:02:07 -0400

> [scott leshke]
> ... one thing that really bothers me the wrong way is the asymmetry
> between a class method call and a class method declaration. In the
> first 'self' is implicitly passed, in the second it has to be
> explicitly accounted for. IMHO, I think it would be preferable to have
> 'self' reserved and be implicit on both invocation and declaration.

Heh heh. If you had started with Python and then moved to C++, you'd
probably be arguing the reverse <wink>.

> ...
> Also, is there any way to duplicate the concept of a destructor in
> Python, ala C++ ?

The special method __del__ is invoked (if it's defined) when an object is
about to be garbage-collected. Note that the latter happens when the
object's reference count drops to zero, and has nothing to do with C++-
like scope rules. You'll probably like Python _much_ better in this


class Example:
def __init__(self, x):
self.val = x

def __repr__(self):
return 'Example(' + `self.val` + ')'

def __del__(self):
print 'deleting instance', `self`

and a session using it:

>>> import Example
>>> def create(n): return Example.Example(n)
>>> x = create(1)
>>> x
>>> y = create(2)
>>> y
>>> z = y # bumps the _object_ ref count to 2
>>> del x # destroy only binding to x's object
deleting instance Example(1)
>>> del y # destroy one of the two bindings to y's object
>>> del z # and destroy the other binding
deleting instance Example(2)

constructively y'rs - tim

Tim Peters
not speaking for Kendall Square Research Corp