What shouldn't be a method function

July 2, 2005

I dislike excessive religion, so I don't hold to the view that everything should be a method function. Fortunately, Python lets me indulge this view.

My rule of thumb is that any method function that doesn't use its self argument to get at things gets made into a normal module level function. This reduces clutter on the class (and in the source code) and makes what's really going on clear to me and any later readers.

Of course there are some exceptions, such as:

  • the function is only sometimes self-less; some implementations have to use self.
  • the function varies on a per-class basis. This is a good case for StaticMethodUse.
  • the function is a generic interface to multiple classes of objects, especially when they're spread out over multiple modules.

For me, most of the functions that wind up being de-method-ized this way are internal helper functions. If a bunch of public interface functions turn out to not use self, it's usually because my design of the object's interface is bad.

Written on 02 July 2005.
« Why this site has almost no design
Checking for dead DNSBls »

Page tools: View Source, Add Comment.
Login: Password:
Atom Syndication: Recent Comments.

Last modified: Sat Jul 2 01:21:03 2005
This dinky wiki is brought to you by the Insane Hackers Guild, Python sub-branch.