A5: Class Folders

This is a written assignment similar to Assignment 2. Once gain you will diagram call frames. But this time we are more concerned with folders: both object and class folders. The purpose of this assignment is to help you understand the issues with name resolution that we discussed in class.

This assignment is going to overlap Assignment 6. That means we will be posting Assignment 6 before this assignment is due. That is because Assignment 6 is a longer assignment and it must be completed well before the second prelim (as the material is on the prelim). As this is a written assignment, and you already have a lot of experience with call frames, this should not be a problem.

Authors: W. White, L. Lee, and S. Marschner

Learning Objectives

This assignment is designed to help you understand the following concepts.

  • How to draw object folders to represent the contents of an instance.
  • How to draw class folders to represent the shared content of all instances.
  • How object and class folders are used to resolve naming issues.
  • How method calls are similar to function calls.

Table of Contents


Academic Integrity and Collaboration

This assignment is a modified version of an assignment given in a previous semester. Please do this assignment without consulting (or seeking) previous solutions. Consulting any prior solution is a violation of CS1110’s academic integrity policy.

We also ask that you do not enable violations of academic policy. Do not post your solutions to Pastebin, GitHub, or any other publicly accessible site.

Collaboration Policy

You may do this assignment with one other person. This does not have to be the same person that you partnered with in the previous assignment. You are free to change partners after every assignment.

When you upload your pdf to Gradescope, you will be given the option to add a person to the submission. You may find these instructions helpful. You also must work together. It is against the rules for one person to to fill out this assignment and just have the other person put their name at it. Other than your partner, you may not collaborate with anyone else on this assignment.

The Python Tutor

By now, everyone is very familiar with the Python Tutor. Once again, using the Tutor makes parts of (but not all of) this assignment easy. We are perfectly okay with this. It is a good way to help you understand the formalism if you are particularly lost.

If you used the Python Tutor to help you with part of the problem, please cite this (for each part that you used it) in your submission. We will not take off any points if you use the Tutor and cite it. We only want a citation to be consistent with our academic integrity policy.

Assignment Help

Even though this is just a written assignment, some of you may still have some difficulty with it. You might not completely understand how function calls work or the purpose of folders. If you are lost, please see someone immediately. This can be the course instructor, a TA, or a consultant. Do not wait until the last minute, as the course staff can get overwhelmed just before an assignment deadline. See the office hours page for more information.


Diagramming Conventions

You are already familiar with most of diagramming conventions from Assignment 2. Our new conventions build on top of these. All of our examples below use the following class definition.

1
2
3
4
5
6
7
8
class Example(object): 
    CONST=3

    def __init__(self):
        pass
        
    def foo(self):
        return 42

The line numbers to the left are used in the method call frames, just as in Assignment 2.

Diagramming Class Folders

When you define a class, Python creates a special folder called a class folder that stores the information about that class. Technically, Python also creates a global variable with the same name as the class and puts the name of that folder in the global variable; that is what you see when you put the class above in the Python Tutor.

To simplify things, we do not want you to show the variable in global space (just like we never ask you to show the variable for a function name in global space). We just want you create a folder in the heap, and put the name of the class in the tab. We also want you to put the tab on the right so that we can tell this is a class folder. This is illustrated below.

Class Folder

The class folder contains all the methods and any class attributes. In a class definition, any assignment statement outside of a method definition creates a class attribute. In the example above, CONST is a class attribute.

Technically, the methods are also variables themselves. This is what you would see if you used the Python visualizer. However, the contents of this variable are really complicated, and the Python visualizer just writes the method header (the name plus all of its parameters) as its value. Therefore, that is what we want you to write for the methods in a class: the method header (without the def).

The example above works when the class is a base class (e.g. a subclass of class object). Suppose we have a class that is a subclass of a user-defined class, like the example Demo below.

1
2
class Demo(Example): 
    pass

We want to indicate this relationship in our class folders. To do this, we put the name of the parent class in parentheses, as shown below. Note that this class has no methods or class attributes in it, because there are none in the class definition.

Subclass Folder

Diagramming Object Folders

A object is instance of a class. While there is only one class Example, there may be more than one Example object. Each time you call the constructor, you create a new object. For example, suppose we execute the following statements:

>>> e = Example()
>>> e.x = 10

The constructor for Example creates an empty folder (why?), while the second assignment statement adds an attribute x to the folder. The end result is as follows:

Object Folder

The instance folder only contains attributes added to the instance. It does not contain class attributes, and (for this course) it never contains methods.

Diagramming Method Calls

A method class works almost exactly like a function call. You create the call frame, and plug the arguments into the parameters. You then execute each line in the method, changing the call frame as necessary. Finally, you erase the frame.

The only real difference is the function name that goes in the upper left box. We want both the name of the class and the method name in this box. For example. for the method call

>>> e.foo()

the initial diagram for the call frame is as follows

Method Call

Note the name is not foo in the top left folder, but Example.foo<. That tells us to use the version of foo that is stored in the class folder Example.

Diagramming Constructor Calls

A constructor is just a function, so Python should create a call frame when it is called. However, this function is built-in, so we do not really know what it looks like. You will notice that the Python visualizer does not create a call frame for the constructor either. However, it does create a call frame for the initializer __init__, which the constructor uses as a helper.

We are going to do it the way that the Python visualizer does it. Remember the 4 steps of a constructor call:

  1. It creates a new empty folder of that class.
  2. It puts the folder into the heap
  3. It executes the method __init__ (creating a call frame)
  4. It returns the folder name

In our diagram, we combine steps 1 and 2 (because otherwise we do not know where to put the folder). We also combine step 4 with the erasing step of __init__ even though __init__ has no return variable. For example, if we executed the constructor

>>> e = Example()

The resulting diagram would be as follows.

Constructor Call

The first diagram is the folder creation step, before the method __init__ is called. The next two diagrams are for the call frame for __init__. The first picture is at the start of the method, and the second picture is the execution of the one and only line of the method (which does nothing). In the final diagram, we erase the frame and assign the value to our variable.


Assignment Instructions

This entire assignment will involve the following two mysterious (and undocumented) classes.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class A(object): 
    x=7
    z=3

    def __init__(self,x,y):
        self.x = y
        self.y = x
    
    def f(self):
        return self.y+self.z
    
    def g(self):
        return self.x+self.z

class B(A): 
    x=6 

    def __init__(self,x,y):
        super().__init__(x,x)
        self.z = y

    def f(self):
        a = self.g()
        return a+self.y

Once again, you should use the line numbers provided when drawing the method call frames. We have kept all of these methods short to cut down on the amount that you have to draw.

Part A: Diagram the two Class Folders

The instructions are in the title for this section. We just want you to draw two class folders. You draw one for the class A and another for the class B. Do not draw global space (even though there are global variables A and B that refer to these class folders). There is also no call frame to draw.

You can complete the class folder for A before the Tuesday, November 1 lecture. The folder for class B requires a minor change from the subclasses lecture, but you can figure it out from the instructions above.

Part B: Diagram the Execution of p = A(2,4)

Draw the execution of the statement

p = A(2,4)

This is a constructor call, so it will involve the creation of a call frame for __init__. As usual, you will need a diagram for when the frame for __init__ created, and another when it is erased. In between, you will diagram the two lines of code that are executed.

However, as described above, you do not draw the call frame right away. You need a step creating the folder first. In particular, you will need to draw how global space and the heap changes with the call frame, just like you did on the prelim. You do not need to add anything to global space beyond what is required for the statement p = A(2,4).
In particular, you do not need a global variable for class A.

You can complete this part before the Tuesday, November 1 lecture.

Part C: Diagram the Execution of q = B(5,1)

This problem is exactly like Part B. We need the evolution of global space, the heap, and the call frame for __init__. The difference this time is that there is a call stack created with more than one frame (just like on the first prelim). You should draw that properly.

You do not need to include p (from Part B) in your diagram of global space.

Part D: Diagram the Execution of r = q.f()

In this problem, q is the variable from Part C. Diagram the evolution of the call frame for method f. This is a normal method call (not a constructor), as described above. If this method calls another method as a helper, you should draw the complete call stack, just like you did in the previous problem.

Your diagrams should include the contents of global space at each step. You do not need to draw the heap, since method f will not change the contents of any folder.


Finishing Touches

When you finish the assignment, put your name and netid at the top of every page. Otherwise, we might lose track of the assignment, and we cannot give you credit for your work. If you are working with a partner, then the partner’s name and netid must be on the assignment as well.

Creating a PDF for Submission

We want you to submit your work as a PDF. If you created your assignment on a computer, this should be relatively easy. Otherwise, you will need to scan your paper as a PDF. The ideal way to do this is with a scanning app on your phone. For iOS, we recommend Scannable. For Android we recommend Genius Scan. Gradescope has provided detailed instructions on how to best scan your assignment to make it readable.

If you do not have a phone capable of supporting these apps, then you will need to find some other way to scan the assignment. If you are on campus, there are scanners in Olin and Uris Library. Note that these phone apps will be the same ones that we will use for students taking an exam online, so you will not be able to take an online exam if you cannot get those apps working.

Your solution should be a single PDF file. You cannot submit multiple PDF files. Provided that you have used the apps that we recommend, this will be taken care of for you. Otherwise, you may need a tool to combine multiple PDFs together. On Windows, the program PDFtk can merge multiple PDF files into one file. In macOS, the built-in application Preview can be used to merge PDF files.

Uploading to Gradescope

Written, PDF-based assignments will be handled this semester through Gradescope. This will allow us to mark up your PDFs with feedback and allow you to see them right away. It is not a perfect service and it has many issues, but has the advantage that you can verify your submission to make sure it is readable, complete, and that all questions are answered. You cannot do that in CMS.

If you have scanned the assignment on your phone, you will need to transfer it from your phone to gradescope. The easiest ways to do this are by mailing yourself the assignment, or uploading it to a cloud-based drive such as Google Drive, DropBox, or OneDrive. Again, see the detailed instructions for uploading your assignment to Gradescope.

You must upload your submission to Gradescope by Friday, November 3. Remember to add your partner when you submit. Unlike the last assignment, you only get one submission for this assignment. We do accept late submissions (up to Saturday) for a penalty of 10% per day. This is roughly a letter grade for each day late. Remember, once you upload, you should view your submission to make sure it is readable.

Completing the Survey

Once again, this assignment comes with a survey on CMS. This survey asks how long you spent on the assignment, your impression of the difficulty, and what could be done to improve it.

Please try to complete the survey within a day of turning in this assignment. Remember that participation in surveys compromise 1% of your final grade. We also ask that you be honest in your answers.