[Python-3000] Removing 'self' from method definitions

Guido van Rossum guido at python.org
Tue Apr 18 10:49:28 CEST 2006


On 4/18/06, Greg Ewing <greg.ewing at canterbury.ac.nz> wrote:
> The problem is that there's no way for Python to know
> which class the method is "in", in the sense required
> here.
>
> That could be fixed by giving functions defined inside
> a class statement an attribute referring back to the
> class. It would create a circular reference, but that's
> not so much of a problem these days.

This begs two questions (at least):

- How would the function be given the attribute? At the time the
function is being defined the class doesn't exist yet; and by the time
the class exists, the function may be wrapped in any number of layers
of decorators which may or may not pass on attribute assignment (most
implementations of decorators I've seen don't -- at best they copy the
attributes that already exist on the function into the wrapper).

- How would the code representing the function body access the
attribute? By the time the code is executing all that is known are the
argument values and the globals. There's no reference to the function
object itself (nor to the bound method). (Nor to the class, for that
matter.)

You may be able to come up with suitable answers -- but IMO these
questions (again) point out that the coupling between methods and the
class to which they belong is much looser in Python than in most other
languages. This started as a hack when classic classes were invented
as an afterthought; but this has now become a fundamental and
desirable feature of the language.

--
--Guido van Rossum (home page: http://www.python.org/~guido/)


More information about the Python-3000 mailing list