class A: __metaclass__ = Selfless def __init__(a , b): self.a = a self.b = b
Is made to work from this example.
One common critic to Python is the need of the explicit "self" parameter declaration on function Methods. No matter how this design is optimal in terms of internal consistency, keeping things explicit, and reducing the keyword count, people keep complaining about it.
Given the access granted to the inner workings of the Language, it is easy to arrange an alternate mechanism for class methods that could preclude the declaration of a first parameter to a method. Therefore, people who are seeing complaining could try some shots of "selfless" Python.
My approach here is just as follows: I create a metaclass that uses an alternate way to fetch function methods inside the classes. With the normal "type" metaclass - i.e. normal "new style" classes - any functions declared in a class body are substituted, at class creation time, for a descriptor. When the attribute with the function name in a class, or instance of it, is retrieved, this descriptor creates a method object that wraps the original function. (And yes, the method object is created at attribute access time). This method object, when called, prepends the "instance" object as the first parameter in the list, and forwards the call to the wrapped function.
In this "selfless" proof of concept, I change the functions in the class body for descriptor objects, but descriptors that behave differently: instead of wrapping the function in a method object, they recreate the function when the method is acessed.
A new function object is created with the same code object, the same name, the same closure, but, with a different global dictionary. The global dictionary of the original function is copied and changed to include an entry named "self" which points to the object instance. (Or "None" if the function is retrieved from the class, not from an instance).
Therefore, inside these selfless methods, the name "self" can be used without ever being declared: it will exist as a global vairable at execution time, and point to the instance. Just like the parameter self exists as a local variable and points to the instance in regular Python methods.
This should not be used in production code - it is a good proof of concept - but of course there should be lots of strange and inconsistent behavior associeated with it. One of which is the fact methods can not be used "unbounded" as one does with ordinary python methods, explicitly passing an instance object as the first parameter.
Asa final consideration, Pedro Werneck had an implementation of this "feature" before here: http://www.python.org.br/wiki/NoSelf - but unlike Werneck's example, I don't fiddle with the bytecode or anyother thing to "rebuild" the bytecode so that references to an unexisting local self variable are rewritten, and "self" is artificially inserted as a local variable at runtime. The work presented here is an order of magnitude simpler, and possibly less subject to colateral effetcs.
Nonetheless: I warn again - this is just a toy! Don't use this in "real life"!
Other than that, it works finner and in a simpler way than I expected.
# -*- coding: utf-8 -*- from types import FunctionType """ Changes classes so that each method has its instances changed so that "self" is a global variable pointing to the instance. """ class SelflessDescriptor(object): def __init__(self, func): self.func = func def __get__(self, instance, class_): new_globals = self.func.func_globals.copy() new_globals["self"] = instance new_func = FunctionType(self.func.func_code, new_globals, self.func.func_name, self.func.func_defaults, self.func.func_closure) return new_func class Selfless(type): def __new__(cls, name, bases, dict_): for key, val in dict_.items(): if isinstance(val, FunctionType): dict_[key] = SelflessDescriptor(val) return type(name, bases, dict_) if __name__ == "__main__": __metaclass__ = Selfless class A: def __init__(a, b): self.a = a self.b = b a = A("Hello", "World!") print a.a, a.b