Classifying What You’ve Learned

In this chapter, you learned the following:

  • In object-oriented languages, new types are defined by creating classes. Classes support encapsulation; in other words, they combine data and the operations on it so that other parts of the program can ignore implementation details.

  • Classes also support polymorphism. If two classes have methods that work the same way, instances of those classes can replace one another without the rest of the program being affected. This enables “plug-and-play” programming, in which one piece of code can perform different operations depending on the objects it is operating on.

  • Finally, new classes can be defined by inheriting features from existing ones. The new class can override the features of its parent and/or add new features.

  • When a method is defined in a class, its first argument must be a variable that represents the object the method is being called on. By convention, this argument is called self.

  • Some methods have special predefined meanings in Python; to signal this, their names begin and end with two underscores. Some of these methods are called when constructing objects (__init__) or converting them to strings (__str__ and __repr__); others, like __add__ and __sub__, are used to imitate arithmetic.

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

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