All this self dot is making me...

Jim Rudnicki jdrudnicki at yahoo.com
Wed Jun 2 04:59:35 EDT 1999


To the beginner, having to type all these self. 's in class methods seems a
bit painful.  So all three of these methods below do the same thing.  That
leaves one to wonder "why have classes at all?"  A little thought and I
remember that this same thing is happening in C++: the compiler is adding a
this pointer to the passed parameters.  So Func and fooFunc are the same
thing.  The only difference between a method and a class method is
scope/access rules and tacking on this pointers, which in C++ is done by the
compiler. Correct?

So what has me perplexed is why all the self. 's?  Is this a namespace issue
or maybe just to expensive to do at runtime?

For those of us who have less cartilage left in their hands, would it be
acceptable to use "my." or better "me."  Or is self. strongly ingrained?
The two less characters add up and the keys are on stronger fingers.

nite,
Jim

def Func( this, a, b ):
  this.x = a + b

class Foo:
  def __init__( self ):
    self.x = 0
    self.f = Func
  def fooFunc( me, a, b ):
    me.x = a + b
  #if it was C++
  # fooFunc( a, b ) {
  # x = a + b; }

# who needs class methods?
z1 = Foo()
Func( z1, 5, 7 )
print z1.x
# it doesn't really matter if the method is in a class
z2 = Foo()
z2.f( z2, 5, 7 )
print z2.x
# this is the same as z1 approach
z3 = Foo()
z3.fooFunc( 5, 7 )
print z3.x






More information about the Python-list mailing list