Main Content

Class, static and unbound variables

Archive - Originally posted on "The Horse's Mouth" - 2005-10-25 14:47:07 - Graham Ellis

In Object Oriented programming, you'll have certain named blocks of code (usually known as methods) that you can perform on specific objects (type A), and others that you can perform on all the objects of a particular type (type B).

Example.

If I had a class called sandwich, I might have one method called getFilling and another called getBread which return me information as to whether a particular sandwich is Beef or Cheese, and whether it's on brown or white bread. These methods are both Type A as their actions will vary depending on which sandwich they're run.

However, if I have a method called getCount that returns me a total of the number of sandwiches I have, then it's going to return me the same value whether I call it on my "Beef and Stilton on White", or on a Plain cheese sandwich in brown bread. So that's an example of a Type B.

Type B methods are sometimes known as Class methods since they apply to the class as a whole. You'll also find that Java programmers refer to them as static methods, and Python fans call them unbound methods since they're not attached (bound) to any particular single instance of an object.

Type A methods, then, may be known as Object methods as they apply to an individual object, dynamic methods as they change in behaviour depending on which particular object they're called on, or bound methods because their behaviour is bound to a certain object.