Object oriented programming in python
- Like many aspects of programming it is necessary to learn the concepts of object oriented programming before you can use them effectively.
- So approach this chapter as a way to learn some terms and concepts and work through a few simple examples to lay a foundation for future learning.
- Throughout the rest of the book we will be using objects in many of the programs but we won’t be building our own new objects in the programs.
- Python is an object-oriented programming language, which means that it manipulates and works with data structures called objects.
- Objects can be anything that could be named in Python – integers, functions, floats, strings, classes, methods, etc.
- These objects have equal status in Python. They can be used anywhere an object is required.
- You can assign them to variables, lists, or dictionaries. They can also be passed as arguments. Every Python object is a class.
- In object-oriented programming, the term object loosely means a collection of data (attributes)
with a set of methods for accessing and manipulating those data.
- There are several reasons for
using objects instead of sticking with global variables and functions. Some of the most important benefits of objects include the following:
- Polymorphism: You can use the same operations on objects of different classes, and
they will work as if “by magic.”
- Encapsulation: You hide unimportant details of how objects work from the outside
- Inheritance: You can create specialized classes of objects from general ones.