Multiple Choice
True or False: A class definition provides a pattern for creating objects, but doesn’t make any objects itself.
True
False
True or False: All instances of a class have the same attribute values.
True
False
True or False: An object’s attribute values cannot be accessed from outside the class.
True
False
What is the difference between a class and an object?
A class is a collection of objects
A class is a blueprint; an object is a specific instance of that blueprint
They are the same in Python
An object can contain classes, but not the other way around
True or False: Because class definitions have attributes, local variables are not allowed inside method definitions.
True
False
What does it mean to “instantiate” a class?
Define the class
Import a module
Create an object from a class
Define attributes
True or False: The constructor of a class is only called once in a program, no matter how many objects of that class are constructed.
True
False
The first parameter of any method is _____ and it is given a reference to the object the method was called on.
me
self
init
this
current
An instance of a class is stored in the:
stack
heap
output
True or False: Once attributes are initialized in the initializer/constructor, the values cannot be changed.
True
False
True or False: A class definition introduces a new data type, or type of object, in your program.
True
False
SOLUTIONS
True
False
False
A class is a blueprint; an object is a specific instance of that blueprint
False
Create an object from a class
False
self
heap
False
True
Select All That Apply
Consider the following code listing. Select all lines on which any of the concepts below are found. Select if the concept is not in the code listing.
class Point:
x: float
y: float
def __init__(self, x: float, y: float):
self.x = x
self.y = y
def flip(self) -> None:
temp: float = self.x
self.x = self.y
self.y = temp
def shift_y(self, dy: float) -> None:
self.y += dy
def diff(self) -> float:
return self.x - self.yConstructor Declaration
1
2
5
9
11
Attribute Declaration
2
3
6
7
10
Attribute Initialization
2
3
6
7
10
Method Declaration
1
9
10
14
17
Local Variable Declaration
2
3
6
7
10
Instantiation
1
5
9
10
N/A
SOLUTIONS
Line 5 only
Lines 2 and 3
Lines 6 and 7
Lines 9, 14, and 17
Line 10
N/A
Short Answer
What does
selfrefer to in Python classes?Similar to how a function is first defined then called, a class is first defined then ____.
When a method is called, do you have to pass an argument to the
selfparameter?When is
selfused outside of a class definition?Use the following point class to answer the questions.
class Point: x: float y: float def __init__(self, x: float, y: float): self.x = x self.y = y def flip(self) -> None: temp: float = self.x self.x = self.y self.y = temp def shift_y(self, dy: float) -> None: self.y += dy def diff(self) -> float: return self.x - self.y5.1. Write a line of code to create an explicitly typed instance of the Point class called my_point with an x of 3.7 and y of 2.3.
5.2. Write a line of code to change the value of the my_point variable’s x attribute to 2.0.
5.3. Write a line of code to cause the my_point variable’s y attribute to increase by 1.0 using a method call.
5.4. Write a line of code to declare an explicitly typed variable named x. Initialize x to the result of calling the diff method on my_point.
SOLUTIONS
selfrefers to the current instance of the class that the methods will operate on if those methods are called in the future on an instance of that class.Instantiated
No! When you call the constructor for a class,
selfis automatically made by python in order for the rest of the constructor to finish making the object. In the case of other methods, python knows thatselfis the object that you called the method on, often the variable name that comes before the method call (e.g. formy_point.shift_y(1.0),selfismy_point).selfis not used outside of a class definition. Outside of a class definition you use the name of the variable storing an object to refer to it.
5.1. my_point: Point = Point(x=3.7, y=2.3)
5.2. my_point.x = 2.0
5.3. my_point.shift_y(1.0)
5.4. x: float = my_point.diff()