11.1 Object-oriented programming basics
3.
a.
A procedure, such as
Change venue
, can make changes to the field and protect the field from inappropriate access.
4.
b.
Limiting data access prevents accidental changes and makes relationships between objects clearer.
5.
b.
Correct abstraction makes only necessary information visible. An overheated engine can cause irreparable damage, so an indicator is necessary for the driver.
11.2 Classes and instances
1.
a.
Cat
follows the class keyword at the top of the class definition and is the name of the class.
3.
a.
breed
is an instance attribute defined in __init__()
. The other instance attributes are name
and age
.
4.
c.
CapWords
style, with all words capitalized and no spaces between words, is recommended for class names. "Pet" and "cat" are separate words, so each is capitalized and one directly follows the other.
5.
b.
Two instances,
room_1
and room_3
, are created by calling Rectangle()
, which calls the constructor __init__()
.
6.
b.
room_1
is the instance that called the method area()
, so self
represents the specific instance.
7.
a.
Line 3 is in
__init__()
, which initializes length and the other instance attributes for each new instance.
8.
c.
__init__()
has one required parameter, conventionally called self
, which allows the constructor to access the instance.
9.
b.
order_id
is an instance attribute defined in __init__()
. The other instance attribute is cup_size
.
10.
b.
Changing
order_1.cup_size
does not affect order_3.cup_size. order_1.cup_size
changes to
8
and order_3.cup_size
is still
16
.
11.
a.
order_1
, order_2
, and order_3
all share the class attribute CoffeeOrder.loc
. Changing loc
affects all instances, so Caffeine Cafe
is printed as the location.
12.
b.
Trying to access a class attribute using an instance is unsafe. In
__init__()
, trying to access the class attribute creates a new instance attribute called cls_id
. Though possible, programmers should avoid using the same name for class attributes and instance attributes to avoid confusion.
11.3 Instance methods
2.
b.
self
is the first parameter in __init__()
definition, not an explicit argument. self
should not be included in the call.
3.
c.
The second definition overwrites the first as multiple explicit definitions of
__init__()
aren't allowed. __init__()
requires all parameters, so the first two __init__()
calls for patient_1
and patient_2
are incorrect.
4.
c.
order_1
is originally a coffee without milk or sugar, but the call to change()
adds sugar to the order by assigning order_1.with_sugar
with
True
.
5.
c.
An instance method's first parameter indicates the method can change an instance.
change()
allows a change in whether a coffee order has milk or sugar.
6.
b.
Unindenting
print_order()
would produce an error. An instance method must be defined in the class.
11.4 Overloading operators
1.
c.
__add__()
is a magic method, indicated by the double underscores at the start and end of the method name.
2.
b.
Magic methods are designed to be invoked internally to perform under-the-hood actions to simplify tasks for the user.
3.
c.
__init__()
is defined by the programmer to initialize instance attributes. __str__()
is defined by the programmer to customize an Engineer
instance's string representation.
5.
b.
The addition operation maps the left operand,
acct_a
, to the parameter self
and the right operand, acct_b
, to the parameter other
.
6.
c.
- is a binary operator, so
__sub__()
has two parameters. The parameters' x
and y
attributes are subtracted from each other, and the resulting values are made into a new Pair
.
8.
b.
__le__()
is the magic method for the <= operator. __le__()
takes two parameters for the left and right operand, and returns the correct comparison of the two amount values.
11.5 Using modules with classes
1.
b.
Level
and XP
are classes imported from the character module into the program. Any other class in the character module is not imported.
3.
c.
The number of classes in a module is unlimited, but classes should be grouped in modules to maximize reusability.
4.
b.
The character module is aliased as
game_char
, so character is not defined in the program. game_char
is used in place of character.
5.
c.
game_char
referred to the imported module, but assigning game_char
replaces the imported module with the string
'Ranger'
. game_char
can no longer be used to access classes.