After reading lesson 32, you’ll be able to
- Define a class to simulate a stack
- Use a class with other objects you define
At this point, you know how to create a class. Formally, a class represents an object type in Python. Why do you want to make your own object types in the first place? Because an object type packages a set of properties and a set of behaviors in one data structure. With this nicely packaged data structure, you know that all objects that take on this type are consistent in the set of data that defines them, and consistent in the set of operations that they can perform.
The useful idea behind object types is that you can build upon object types you create to make objects that are more complex.
Consider this
Subdivide each of the following objects into smaller objects, and those into smaller objects, until you can define the smallest object by using a built-in type (int, float, string, bool):
- Snow
- Forest
Answer:
- Snow is made up of snowflakes. Snowflakes have six sides, and are made up of crystals. Crystals are made up of water molecules arranged in a certain configuration (a list).
- A forest is made up of trees. A tree has a trunk and leaves. A trunk has a length (float) and a diameter (float). Leaves have a color (string).