By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,871 Members | 2,575 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,871 IT Pros & Developers. It's quick & easy.

Getting rid of "self."

P: n/a
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
Jul 18 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
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

Jul 18 '05 #2

P: n/a

"BJörn Lindqvist" <bj*****@gmail.com> wrote in message
news:ma**************************************@pyth on.org...
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?

[response]

Having to specify the instance explicitly is something that
Python needs because it isn't a statically typed language.
In a statically typed language, all variables are pre-declared,
so the compiler knows where they all are.

Python's compiler knows about local variables. It
doesn't know where any other variables are, so it
has to search. Including the instance as one of the
method parameters means that the search splits
right at the front: either it starts looking up the
instance, or it goes up the definition chain (usually
empty) to the module namespace and then the
builtins.

Eliminating "self" would mean it would have to
either search the instance before the module and
builtins, or search the module and builtins before
the instance. This is both a performance issue
and a maintenance issue because of the increased
possibility of one shadowing the other.

This is distinct from the issue of how to spell
"self". As another responder has already said,
you can spell it any way you want; it's simply
whatever you choose to call the first paramteter
to the method.

Going the other way, the word "self" could become
a keyword, removing the necessity of specifying it
among the method parameters. While I like the idea,
there's enough dislike of the notion that it's not going
to happen.

John Roth
--
mvh Björn

Jul 18 '05 #3

P: n/a
On Fri, 07 Jan 2005 14:39:09 +0100, BJörn Lindqvist wrote:
It works! exec(magic()) does the needed hi = self.hi.


No it doesn't. Try "hi = 'newValue'" and see what happens.

So the next step is to write an "unmagic" function. So now how do you add
instance variables?

There is no way to avoid "self" *and* not pre-declare variables in some
fashion as belonging to the instance (as declarations, as sigils, what
have you). Given that Python is not, will not, and should not do the
latter, I submit that "self" is, at least for you, the lesser of two
evils. (I don't consider it evil at all, so it isn't such for me; were I
programming in C++ routinely now I'd prefix "this" and dispense with that
ugly "m_" garbage. (One of the things I ***hate*** about C++ culture is
its acceptance of hideously ugly variable names, but now I'm two
parentheticals deep so I probably ought to stop.))
Jul 18 '05 #4

P: n/a
Jeremy Bowers <je**@jerf.org> wrote:
were I programming in C++ routinely now I'd prefix "this" and
dispense with that ugly "m_" garbage. (One of the things I ***hate***
about C++ culture is its acceptance of hideously ugly variable names,
but now I'm two parentheticals deep so I probably ought to stop.))


I'm currently working in a C++ system where they have a wrapper class
that provides some transaction locking functionality for member access.
The colloquial name for the wrapped "this" pointer is self, i.e. they do
"self = wrapper (this)" at the beginning of functions that need it. You
can then do "member" to get the bare access or "self.member" to get the
locking functionality.

It's actually kind of neat, but boy does it play headgames with me when
I switch back and forth between that and Python.
Jul 18 '05 #5

P: n/a
Roy Smith wrote:
It's actually kind of neat, but boy does it play headgames with me when
I switch back and forth between that and Python.


Switching back and forth betwen C++ and Python plays headgames *anyway* }:>

Cheers,
Nick.
Hardware control with Python is nice. . .

--
Nick Coghlan | nc******@email.com | Brisbane, Australia
---------------------------------------------------------------
http://boredomandlaziness.skystorm.net
Jul 18 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.