10.7 Summary

In this chapter, we explored the implementation of our own data types by creating classes. The programmer can take advantage of this ability to create solutions using a data model that closely resembles the problem domain. Classes provide a description of objects (instances of classes) by defining data and methods. Objects are created by calling the constructor method.

Other methods, both accessors and mutators, allow the programmer to communicate with individual objects. We also introduced a number of special methods defined by Python, which in some cases are bound to operators. Finally, we used the Turtle to implement a graphical simulation of planetary movement.

Key Terms

  • acceleration

  • accessor method

  • class

  • class definition object

  • constructor

  • data model

  • dunder methods

  • encapsulation

  • gravitational constant

  • gravity

  • index operator

  • information hiding

  • instance

  • instance data

  • instance variable

  • magic methods

  • method

  • mutator method

  • mass

  • object

  • object-oriented programming

  • state

  • velocity

Python Keywords and Functions

  • _ _add_ _

  • _ _eq_ _

  • _ _ge_ _

  • _ _getitem_ _

  • _ _gt_ _

  • _ _init_ _

  • _ _le_ _

  • _ _lt_ _

  • _ _ne_ _

  • _ _str_ _

  • class

  • self

  • type

Programming Exercises

  1. 10.1   Modify the SolarSystem class to support more than one sun.

  2. 10.2   Modify the Planet class so that the planet does not have the instance variables for x and y but simply uses the x and y location stored in the turtle.

  3. 10.3   Research n-body simulations. Using the ideas from this chapter, implement your own simple n-body simulation.

Design Credits: Calculator Icon made by Smashicons from www.flaticon.com

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.141.165.120