learn python classes and objects

Classes and Objects in Python

Current Status
Not Enrolled
Get Started

Object-oriented vs. procedural programming

Before we go into more detail about object orientation, let’s first look at the difference between procedural programming and object-oriented programming?

Procedural programming

Procedurally oriented programming uses the “top-down” programming style. Problem is formulated as sequential string of subtasks to be done. Thus a free and thus unprotected data flow and a bad illustration of “real” conditions and relations prevail.

Object oriented programming

The object-oriented programming pursues the approach of the “bottom up” programming style. Problems are considered as a composition of a number of objects that interact with each other. This creates the advantage of reusability and encapsulation of data and associated functions.

Classes and Objects

The concept of object-oriented programming is primarily represented by classes and objects. In order to better structure and understand source code, so-called classes are used in object-oriented programming. These in turn have certain properties (“attributes”) and functions (“methods”).

A new class is introduced in Python with the keyword class, followed by the class name and a colon. All subsequent definitions of properties and functions belonging to the class are indented.

Attributes and methods

Attributes describe the data structure of the objects of a class, determine the state of an object. Methods are functions that an object can execute and that determine how an object should react when it receives a message. So attributes and methods are nothing else than variables and functions defined within a class.

Goals of object-oriented programming

Data abstraction through classes

Definition and use of application-specific data types and the operations possible on them in a class. This allows related data to be bundled into one data type. The access to the attributes does not take place thereby directly, but over the methods.

Data encapsulation

Agreeing on data and the functions that manage that data in an unit. Accesses can be restricted using modifiers. In general, the data elements of an object are completely encapsulated (private) and are not accessible to the user. not manipulable for the user. Only over public functions the user can access the object.

Reusability of program parts

The modeling of classes and objects and inheritance, when properly applied, provide code reduction on the one hand, and the formation of more abstract, reusable classes on the other.

Reliability and change friendliness

In the long term, the reuse of existing and already tested application components ensures greater reliability of the system. The modular structure facilitates the extension and modification of the application.

Operator overloading

Through the principle of overloading operators, it is possible to use the operators known in one namespace of a data type (e.g. +,-,*,/,….) in another data type, but with a different meaning there.