Things
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())
b = Thing()
a.set(b)
print(a.x)
b.set(10)
print(a.x.x)
print(a.x.get())
print(3 + a.get().get())
c = a.get()
print(c.x)
a.set(1 - a.get().get())
print(a.x)
c.set(1)
print(a.get().get())
a = Thing()
b = Thing()
a.set(b)
b.set(a)
print(a.x == b)
print(a.x.x == a)
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).
print(a.x.x.x == b)
Next Exercise: Account