Wandering Thoughts archives


What shouldn't be a method function

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.

python/NonMethodFunctions written at 01:21:03; Add Comment

Page tools: See As Normal.
Login: Password:
Atom Syndication: Recent Pages, Recent Comments.

This dinky wiki is brought to you by the Insane Hackers Guild, Python sub-branch.