Skip to ContentGo to accessibility pageKeyboard shortcuts menu
OpenStax Logo

11.1 Object-oriented programming basics

1.
b. Ellis's username represents the post creator.
2.
b. An object models a real-world entity with fields and procedures.
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.
6.
a. Showing only the necessary information reduces clutter and improves the overall experience.

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.
2.
c. pet_1 is an instance of the Cat class created by calling Cat(). The other instance is pet_2.
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

1.
b. Removing the default parameter value makes bp a required parameter.
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.
4.
a. A programmer can overload an operator to customize the built-in operator's function.
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.
7.
a. <, >, and == are overloaded with __lt__(), __gt__(), and __eq__(), respectively.
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.
9.
b. __gt__() overloads the > operator, which makes a comparison and returns True or False .

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.
2.
b. A single class or multiple classes can be imported from a module using the class name(s).
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.
Citation/Attribution

This book may not be used in the training of large language models or otherwise be ingested into large language models or generative AI offerings without OpenStax's permission.

Want to cite, share, or modify this book? This book uses the Creative Commons Attribution License and you must attribute OpenStax.

Attribution information
  • If you are redistributing all or part of this book in a print format, then you must include on every physical page the following attribution:
    Access for free at https://openstax.org/books/introduction-python-programming/pages/1-introduction
  • If you are redistributing all or part of this book in a digital format, then you must include on every digital page view the following attribution:
    Access for free at https://openstax.org/books/introduction-python-programming/pages/1-introduction
Citation information

© Mar 15, 2024 OpenStax. Textbook content produced by OpenStax is licensed under a Creative Commons Attribution License . The OpenStax name, OpenStax logo, OpenStax book covers, OpenStax CNX name, and OpenStax CNX logo are not subject to the Creative Commons license and may not be reproduced without the prior and express written consent of Rice University.

This book utilizes the OpenStax Python Code Runner. The code runner is developed by Wiley and is All Rights Reserved.