All this self dot is making me...

Chad Netzer chad at vision.arc.nasa.gov
Wed Jun 2 14:00:28 EDT 1999


Jim Rudnicki wrote:

> 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.

First of all, you should probably look into an alternate keybard or keyboard
layout, such as Dvorak, if this is a chronic problem.  Also, drink lots of
water; I've found it to be the best defense against keyboard/mouse related
hand problems.  Also, use an abbreviation expansion mode of your editor,
or find a better editor; these are VERY helpful when using descriptive names.

> 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
>
> # who needs class methods?
> z1 = Foo()
> Func( z1, 5, 7 )
> print z1.x

Numeric Python uses this approach (mainly), since these functions can then be
made to work on more than one type of object.  So, if Func() is meant to apply
to a number of objects, this is probably the way to go.  However, it would be
better written as:

def Func(a,b):
   return a + b

So that is doesn't rely on the behavior of "this", which would need to support
attribute assignment.  If you are going to alter an object's attributes, it is often
best to use a method on that object, to "encapsulate" its behavior.

But, if you really just want to save typing, I'd suggest letting the computer help
you, or change some habits, but don't make obfuscated code.  Using "this" instead
of "self" should work; but use it consistently.  I'd suggest the abbrev expansion
is a better solution for you.

Chad Netzer






More information about the Python-list mailing list