Getting rid of "self."
bdesth.quelquechose at free.quelquepart.fr
Fri Jan 7 15:22:00 CET 2005
BJörn Lindqvist a écrit :
> 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?
> 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?
The better way is definitively to forget about black magic and
understand why mandatory 'self' is Good Thing (tm).
(Tip : even when [this|self|@|whatsoever] is not mandatory, using it
makes for much more readable code.)
More information about the Python-list