Getting rid of "self."
Tim Roberts
timr at probo.com
Sun Jan 9 03:05:51 EST 2005
BJörn Lindqvist <bjourne at gmail.com> wrote:
>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.
Does it?
class A:
def __init__(self):
self.hi = "yo"
def meth(self):
exec(magic())
print hi
hi = "baby"
print hi
def other(self):
exec(magic())
print hi
a = A()
a.meth()
a.other()
That's way too fragile to be useful.
--
- Tim Roberts, timr at probo.com
Providenza & Boekelheide, Inc.
More information about the Python-list
mailing list