Chapter 6: Classes
IN THIS CHAPTER
How many jokes begin with a phrase like “A man walks into a bar”? So many that when someone hears that phrase, it is likely that they will assume it is a joke. So, to ruin the joke and speak philosophically, 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 they can perform. And 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 computer program it may be 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 have? Well, as a small subset of the possibilities:
|Home (location, address)||String?|
|Interests||Array of String|
|Possessions (other objects)||Array of object|
|Children||Array of person|
So a man would appear to be a complex data type having a number of properties. 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 pretty abstract, but stay with it: 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 characteristics, but man has some that woman does not and vice versa.
So, considering the original proposition: what is a bar? It is clearly something (object) that can hold (contain) a man. Perhaps it can contain many men. 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, one supposes, or a holder of some kind.
So, the phrase “A man walks into a bar” might be expressed as:
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, like so:
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.”
Okay, so what is 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 may all seem very abstract still, but this way of looking at things seems sensible in that it appears to organize information and provide 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 functions and create convenient modular constructions.
Do you need help with this assignment or any other? We got you! Place your order and leave the rest to our experts.