Python Tutorial
Python - Introduction Python - Syntax Python - Variable Python - Number Python - Casting Python - String Python - Operators Python - Conditions Python - Loops Python - List Python - Tuple Python - Set Python - Dictionary Python - Function Python - Module Python - Date & Time Python - Input Output Python - Error & Exception Python - File Handling
Python OOP
OOP - Introduction OOP - Class Members OOP - Constructor Destructor OOP - Data Hiding OOP - Inheritance OOP - Overriding OOP - Overloading

Python - Object Oriented Programming

Object-oriented programming is a style of coding that allows developers to group similar tasks into classes.

Classes provide a means of bundling data and functionality together.

Creating a new class creates a new type of object, allowing new instances of that type to be made.

Each class instance can have attributes attached to it for maintaining its state.

Class instances can also have methods (defined by its class) for modifying its state.

Overview of OOP Terminology

  • Class - A user-defined prototype for an object that defines a set of attributes that characterize any object of the class. The attributes are data members (class variables and instance variables) and methods, accessed via dot notation.
  • Class variable - A variable that is shared by all instances of a class. Class variables are defined within a class but outside any of the class's methods. Class variables are not used as frequently as instance variables are.
  • Data member - A class variable or instance variable that holds data associated with a class and its objects.
  • Function overloading - The assignment of more than one behavior to a particular function. The operation performed varies by the types of objects or arguments involved.
  • Instance variable - A variable that is defined inside a method and belongs only to the current instance of a class.
  • Inheritance - The transfer of the characteristics of a class to other classes that are derived from it.
  • Instance - An individual object of a certain class. An object obj that belongs to a class Circle, for example, is an instance of the class Circle.
  • Instantiation - The creation of an instance of a class.
  • Method - A special kind of function that is defined in a class definition.
  • Object - A unique instance of a data structure that is defined by its class. An object comprises both data members (class variables and instance variables) and methods.
  • Operator overloading - The assignment of more than one function to a particular operator.

Creating a Class

The class statement creates a new class definition.

The name of the class immediately follows the keyword class followed by a colon as follows:

The class has a documentation string, which can be accessed via ClassName.__doc__.

The self represents the instance of the class. By using the self keyword we can access the attributes and methods of the class in python.

The __init()__ is a special method that is called constructor.

Creating Instance Objects

To create instances of a class, you call the class using class name.

Accessing Attributes

You access the object's attributes using the dot operator (.) with object.

Python Built-In Class Attributes

Every Python class keeps following built-in attributes and they can be accessed using dot operator like any other attribute.

Attribute Description
__dict__ Dictionary containing the class's namespace.
__doc__ Class documentation string or none, if undefined.
__name__ Class name
__module__ Module name in which the class is defined. This attribute is "__main__" in interactive mode.
__bases__ A possibly empty tuple containing the base classes, in the order of their occurrence in the base class list.

Now, putting all the concepts together:

The self keyword

Class methods must have an extra first parameter in method definition.

We do not give a value for this parameter when we call the method, Python provides it.

If we have a method which takes no arguments, then we still have to have one argument - the self.

This is similar to this pointer in C++ and this reference in Java.