Getting rid of "self."

BJörn Lindqvist bjourne at gmail.com
Fri Jan 7 08:39:09 EST 2005


I think it would be cool if you could refer to instance variables
without prefixing with "self." I know noone else thinks like me so
Python will never be changed, but maybe you can already do it with
Python today?

.import sys
.
.def magic():
.    s = ""
.    for var in sys._getframe(1).f_locals["self"].__dict__:
.        s += var + " = self." + var + "\n"
.    return s
.    
.class A:
.    def __init__(self):
.        self.hi = "yo"
.        
.    def meth(self):
.        exec(magic())
.        print hi
.        
.a = A()
.a.meth()

It works! exec(magic()) does the needed hi = self.hi. Not so
impressive in this case but much cooler when there is more instance
variables around. But the solution is very ugly because you have to
write exec(magic()) in every method. So I'm asking here if someone
knows a better way, maybe using decorators or metaclasses or other
black magic?


-- 
mvh Björn



More information about the Python-list mailing list