Getting rid of "self."

Bruno Desthuilliers bdesth.quelquechose at free.quelquepart.fr
Fri Jan 7 09:22:00 EST 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?
> 
(snip code)
> 
> 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.)

Bruno




More information about the Python-list mailing list