concept stack in category python

This is an excerpt from Manning's book Get Programming: Learn to code with Python.
4. A thing that holds clear liquid spilled on a rectangular device I use to call/text/watch cat videos and on a stack of thin, white, flimsy things with black-and-white text on them, and now my rectangular device is broken, and my things with black-and-white text are ruined.
The stack will be represented using a list. You can decide that initially a stack is empty, so you initialize a data attribute for the Stack object by using self.stack = [].
Now that you have a Stack object, you can add any other type of object to the stack, not just atomic objects (int, float, or bool). You can add objects of a type that you created.
You wrote a class that represented a Circle object in lesson 31, so now you can create a stack of circles. Listing 32.3 shows you the code to do this. This is similar to the way you added pancakes in listing 32.2. The only difference is that instead of strings representing pancake flavors, you now have to initialize a circle object before adding it to the stack. If you’re running the following listing, you’ll have to copy the code that defines a Circle object into the same file so that Python knows what a Circle is.
Listing 32.3. Making a Stack object and adding Circle objects to it
circles = Stack() #1 one_circle = Circle() #2 one_circle.change_radius(2) #2 circles.add_one(one_circle) #2 for i in range(5): #3 one_circle = Circle() #4 one_circle.change_radius(1) #4 circles.add_one(one_circle) #4 print(circles.size()) #5 circles.prettyprint() #6
Figure 32.2. The circle with a radius of 2 is at the bottom because it’s added first. Then you make a new circle with a radius of 1, five times, and add each one to the stack.
![]()
You may also notice that you have a method in the Stack class named add_many. Instead of a loop that adds one circle at a time, suppose you create one circle with radius 1 and call add_many on the stack with this object’s properties, as in the following listing, and illustrated in figure 32.3.
Listing 32.4. Making a Stack object and adding the same circle object many times
circles = Stack() #1 one_circle = Circle() #1 one_circle.change_radius(2) #1 circles.add_one(one_circle) #1 one_circle = Circle() #2 one_circle.change_radius(1) #2 circles.add_many(one_circle, 5) #3 print(circles.size()) #4 circles.prettyprint() #5