Things

The questions below are due on Sunday August 04, 2024; 10:00:00 PM.
 
You are not logged in.

Please Log In for full access to the web site.
Note that this link will take you to an external site (https://shimmer.mit.edu) to authenticate, and then you will be redirected back to this page.

1) Thing Class

Below is a Python program, split up into several pieces. After each print statement, provide the type and value of the object that will be printed to the screen.

  • If evaluating the print statement in question would cause an error, select "error" as the type and write error (without quotes) in the box.
  • If the value of an expression is a function or class, select the appropriate type and write the name of the function or class in the box.
  • If the value is an instance, select "instance" and write the (case-sensitive) Class name in the box.

We encourage you to draw a diagram of the instances and their attribute values and update it as you work your way through the transcript. In the readings you might find the sections on attributes and methods helpful.

class Thing:
  def set(self, v):
      self.x = v
  def get(self):
      return self.x

a = Thing()
a.x = 3
print(a.get())

Type: 
Value: 

b = Thing()
a.set(b)
print(a.x)

Type: 
Value: 

b.set(10)
print(a.x.x)

Type: 
Value: 

print(a.x.get())

Type: 
Value: 

print(3 + a.get().get())

Type: 
Value: 

c = a.get()
print(c.x)

Type: 
Value: 

a.set(1 - a.get().get())
print(a.x)

Type: 
Value: 

c.set(1)
print(a.get().get())

Type: 
Value: 

a = Thing()
b = Thing()
a.set(b)
b.set(a)
print(a.x == b)

Type: 
Value: 

print(a.x.x == a)

Type: 
Value: 

print(a.x.x is a)

The is operator determines whether or not two objects are the same object in memory (not just that they have equal value).

Type: 
Value: 

print(a.x.x.x == b)

Type: 
Value: 

Next Exercise: Account

Back to exercises