Do I always have to write "self." ?

Will Ware wware at world.std.com
Fri Apr 28 14:16:35 CEST 2000


Louis M. Pecora (pecora at anvil.nrl.navy.mil) wrote:
> I'm sure some way of declaring variables 'local' could be done in the
> class defintion, but I doubt Guido would do that now.

It wouldn't be too hard to write a preprocessor that would do this
for you. You might put tags in your code such as
#Locals: foo bar xyzzy
and
#Endlocals
so that the translation process for foo, bar, and xyzzy would be limited
to the method where they are used. If you promise yourself never to use
a 'def' inside a method definition, you could omit the '#Endlocals' tag,
and just look for the next line with the regular expression 'def ' in it.

> You mean by doing something like
> s=self
> at the beginning of each class method?

The choice to use the word 'self' is not imposed by Python, it's just
a convention for readability. You can use 's' instead if you wish.

def quadratic(self):
  det = self.b ** 2 - 4 * self.a * self.c
  return ((-self.b + math.sqrt(det)) / (2 * self.a),
          (-self.b - math.sqrt(det)) / (2 * self.a))

versus

def quadratic(s):
  det = s.b ** 2 - 4 * s.a * s.c
  return ((-s.b + math.sqrt(det)) / (2 * s.a),
          (-s.b - math.sqrt(det)) / (2 * s.a))
-- 
 - - - - - - - - - - - - - - - - - - - - - - - -
Resistance is futile. Capacitance is efficacious.
Will Ware	email:    wware @ world.std.com



More information about the Python-list mailing list