A Casual Introduction to Classes

How many jokes begin with a phrase like “A man walks into a bar?” So many jokes begin with this line that when someone hears that phrase, they will assume whatever comes next is a joke. But what is a man, what is a bar, and what does walking entail? Walking seems to be something that a man can do, an action he can perform. A bar is a place where a man can walk. Can a man do anything else but walk? Is a bar the only place a man can walk to?

It seems silly to examine a sentence in that way, but in the context of a com­puter program, it is more meaningful. Imagine that this discussion involves a computer game or simulation. A man now represents some kind of thing or object that is manipulated by the program. A man has properties and things it can do, which is to say operations it can perform. What properties does a man object have? See Table 6.1 for a small subset of the possibilities.

A man would appear to be a complex data type having a number of prop­erties. Note especially that a man can have a property or characteristic called spouse. A spouse is something called a person; so is a man, really. This is ab­stract, but consider that a man is a person, and perhaps some of the characteristics of a man are really those of (i.e., inherited from) a person. In fact, it would appear that most of them are. The only thing that distinguishes a man from other persons would (from the list above) be sex, which would be (perhaps) false for a man and true for a woman, another kind of person.

Imagine that there is a whole class of things called person that have most of these properties. A man could be derived from this, since man has many of these properties in common. A woman could be another class, perhaps having a few different properties. A man could have, for example, a “date of last prostate exam” as a property, but a woman could not. A woman could have a “date of last pap smear,” but a man could not. At some point, person has many common char­acteristics, but man has some that woman does not and vice versa.

Let us consider the original proposition: what is a bar? It is clearly a thing (object) that can hold (contain) a man. Perhaps it can contain many men. Can it contain women? Why not? If a person has to be either a man or a woman, then a bar can contain some number of persons. A bar is a class of objects that can hold or contain some number of persons. It would be a container class or a holder of some kind.

The phrase “A man walks into a bar” might be expressed as follows:

aMan.walksInto (aBar)

where aMan is a particular man (a specific instance of a man class) and aBar is a specific instance of a class of objects known as bar. This man has a Name, which is to say that one of the properties that a man has is a Name, and this is really just a variable. Since each individual man has a Name there has to be a way of getting at (accessing) each one. It is done through each instance:

print (aMan.Name)        # Accessing /printing the name.

aMan.Name = “Ted Smith” # Assigning to the name.

Using this syntax, the dot (.) is placed after the name of the instance. The syntax aMan.Name means “look at the variable aMan, which is an instance of man, for a property called Name.”

What is the meaning of walksInto in the above expression aMan. walksInto(aBar)? Considering the syntax just described, it would appear to be a function that was a part of the definition of man. It takes one parameter, which is something having the type bar.

This way of looking at the “man walks into a bar” scenario seems sensible in that it organizes information and provides a clear and formal way to access it and manipulate it. This discussion has been a metaphor for the concept of a class and the ideas behind object orientation, two key elements of modern programming structures. Python permits the programmer to define classes like the man or bar objects previously described, and to use them to encapsulate variables and func­tions and create convenient modular constructions.


Source: Parker James R. (2021), Python: An Introduction to Programming, Mercury Learning and Information; Second edition.

Leave a Reply

Your email address will not be published. Required fields are marked *