Think Python How to Think Like a Computer Scientist
Download 1.04 Mb. Pdf ko'rish
|
thinkpython
- Bu sahifa navigatsiya:
- 17.12. Exercises 169 object-oriented programming
- Exercise 17.7
17.11
Glossary object-oriented language: A language that provides features, such as user-defined classes and method syntax, that facilitate object-oriented programming. 17.12. Exercises 169 object-oriented programming: A style of programming in which data and the operations that ma- nipulate it are organized into classes and methods. method: A function that is defined inside a class definition and is invoked on instances of that class. subject: The object a method is invoked on. operator overloading: Changing the behavior of an operator like + so it works with a user-defined type. type-based dispatch: A programming pattern that checks the type of an operand and invokes dif- ferent functions for different types. polymorphic: Pertaining to a function that can work with more than one type. 17.12 Exercises Exercise 17.6 This exercise is a cautionary tale about one of the most common, and difficult to find, errors in Python. 1. Write a definition for a class named Kangaroo with the following methods: (a) An __init__ method that initializes an attribute named pouch_contents to an empty list. (b) A method named put_in_pouch that takes an object of any type and adds it to pouch_contents . (c) A __str__ method that returns a string representation of the Kangaroo object and the contents of the pouch. Test your code by creating two Kangaroo objects, assigning them to variables named kanga and roo, and then adding roo to the contents of kanga’s pouch. 2. Download thinkpython.com/code/BadKangaroo.py. It contains a solution to the previous problem with one big, nasty bug. Find and fix the bug. If you get stuck, you can download thinkpython.com/code/GoodKangaroo.py, which ex- plains the problem and demonstrates a solution. Exercise 17.7 Visual is a Python module that provides 3-D graphics. It is not always included in a Python installation, so you might have to install it from your software repository or, if it’s not there, from vpython.org. The following example creates a 3-D space that is 256 units wide, long and high, and sets the “center” to be the point (128, 128, 128). Then it draws a blue sphere. from visual import * scene.range = (256, 256, 256) scene.center = (128, 128, 128) color = (0.1, 0.1, 0.9) # mostly blue sphere(pos=scene.center, radius=128, color=color) |
Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling
ma'muriyatiga murojaat qiling