Why self?
Mark McEahern
marklists at mceahern.com
Sat Jul 6 08:05:25 EDT 2002
[Matt Gerrans]
> In Python, I think the required "self" reference in methods is a
> case where the solution is worse than the problem it solves.
What problem do you think it solves?
> Since
> indentation is the method of scoping in Python, this addition of
> five characters to each instance variable is particularly annoying
> -- a one-line statement that contains several instance variables
> can easily become unmanageable.
I personally like the explicit self reference. I think it's a good idea.
I'm not sure I can explain why, but I'll try...
Python is a dynamic language. So you can do stuff like this:
class C:pass
c = C()
def x(self):
if hasattr(self, "name"):
print self.name
else:
print "I don't have a name yet."
C.hello = x
# These two are now equivalent:
c.hello()
C.hello(c)
How is indentation supposed to distinguish def x above as a function from an
unbound method from a bound method?
I think if you consider the full implications of the above example, you'll
understand and perhaps be able to better express than I can why explicit
reference to self in bound methods (is that the right term?) is a good
thing.
// mark
-
More information about the Python-list
mailing list