"self" vs other names

Greg Weeks weeks at vitus.scs.agilent.com
Thu Sep 27 22:11:36 CEST 2001


When using "self" for the initial method argument, I found myself choosing
short attribute names to offset the five characters taken up by "self.".
(Otherwise my code lines got larger than I like.)

I eventually ended up using single-character attributes names, at which
point I realized that I needed help.  So, with all the trepidation that you
might imagine, I switched from "self" to "me".  (I also tried "I", but that
looked silly.)  I realize that two characters shouldn't matter.  But it
turns out that I distinctly preferred it.

*THIS IS OF COURSE A MATTER OF TASTE*.  Here then is a taste test, a method
written first with "me" and then with "self":

    ###########################################################################

    def kill(me):
	try:
	    kill(me.pid, SIGTERM)
	    log("%s %s (%d) <= SIGTERM" % (me.cell, me.datum, me.pid))
	except OSError:
	    log("%s %s (%d) already DEAD" % (me.cell, me.datum, me.pid))
	me.event.wait(10)			# PID PROCESS IS PROBABLY DEAD

	if not me.event.isSet():
	    try:
		kill(me.pid, SIGKILL)
		log("%s %s (%d) <= SIGKILL!!!!" % (me.cell, me.datum, me.pid))
	    except OSError:
		log("%s %s (%d) already DEAD" % (me.cell, me.datum, me.pid))
	me.event.wait()				# PID PROCESS IS DEAD

	me.event.clear()			# RUN() CAN DETECT THE KILL

    ###########################################################################

    def kill(self):
	try:
	    kill(self.pid, SIGTERM)
	    log("%s %s (%d) <= SIGTERM" % (self.cell, self.datum, self.pid))
	except OSError:
	    log("%s %s (%d) already DEAD" % (self.cell, self.datum, self.pid))
	self.event.wait(10)			# PID PROCESS IS PROBABLY DEAD

	if not self.event.isSet():
	    try:
		kill(self.pid, SIGKILL)
		log("%s %s (%d) <= SIGKILL!!!!" % (self.cell, self.datum, self.pid))
	    except OSError:
		log("%s %s (%d) already DEAD" % (self.cell, self.datum, self.pid))
	self.event.wait()				# PID PROCESS IS DEAD

	self.event.clear()			# RUN() CAN DETECT THE KILL

    ###########################################################################

Again, which code reads more easily is a matter of taste.  I just thought
I'd mention the possibility.


Greg



More information about the Python-list mailing list