Registered: Nov, 2007
Re: Get rid of self (was: A Response to Bruce Eckel)
Posted: Dec 13, 2007 4:59 PM
> > There is a big difference. Explicit self violates the
> > principles of consistency and visibility (whitespace
> > doesn't):
> > Class functions are defined with x parameters, but are
> > called with x-1 parameters.
> No they are not; they are called with x parameters too,
> only the syntax is different for the first argument.
obj.meth(*args, **kwds) is typically
> equivalent to
Well, from an HCI (user) perspective, class functions are defined with x parameters, and are called with x-1 parameters.
> Are you aware of the fact that methods can be added to
> classes dynamically ?
No I was not, but I should have been. It clearly follows from the conceptual model.
I still feel there must be a better way to handle this inconsistency.
So, the problem is that, when a function is encapsulated within a class, it requires a reference to the surrounding object.
Current Solution: The reference comes through the first parameter (e.g., 'self'), which is required at function definition, but not at function invocation. This discrepancy causes unnecessary usability breakdowns.
Possible Solution: Have the keyword 'self' be a reserved word. Have every instantiated object automatically contain a 'self' reference pointing to the object itself (similar to Java's 'this'). Have free functions that are being defined to be dynamically added to a class use the 'nonlocal' mechanism (statement) to define 'self'. Have each function defined within a class, implicitly use the same mechanism to reach out and access that reference automatically (no parameter list "pollution"). Wouldn't that work?
If so, this solution may be cleaner and may fix the usability problem with 'self' being passed as a parameter. Usability (learnability, consistency, etc.) is a major reason that Python is such a successful language. This is key.
Just a thought.