11.1 Object-oriented programming basics
Change venue
, can make changes to the field and protect the field from inappropriate access.
11.2 Classes and instances
Cat
follows the class keyword at the top of the class definition and is the name of the class.
breed
is an instance attribute defined in __init__()
. The other instance attributes are name
and age
.
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.
room_1
and room_3
, are created by calling Rectangle()
, which calls the constructor __init__()
.
room_1
is the instance that called the method area()
, so self
represents the specific instance.
__init__()
, which initializes length and the other instance attributes for each new instance.
__init__()
has one required parameter, conventionally called self
, which allows the constructor to access the instance.
order_id
is an instance attribute defined in __init__()
. The other instance attribute is cup_size
.
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
.
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.
__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
self
is the first parameter in __init__()
definition, not an explicit argument. self
should not be included in the call.
__init__()
aren't allowed. __init__()
requires all parameters, so the first two __init__()
calls for patient_1
and patient_2
are incorrect.
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
.
change()
allows a change in whether a coffee order has milk or sugar.
print_order()
would produce an error. An instance method must be defined in the class.
11.4 Overloading operators
__add__()
is a magic method, indicated by the double underscores at the start and end of the method name.
__init__()
is defined by the programmer to initialize instance attributes. __str__()
is defined by the programmer to customize an Engineer
instance's string representation.
acct_a
, to the parameter self
and the right operand, acct_b
, to the parameter other
.
__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
.
__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
Level
and XP
are classes imported from the character module into the program. Any other class in the character module is not imported.
game_char
, so character is not defined in the program. game_char
is used in place of character.
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.