View on GitHub

cs-concepts

Understanding computer science concepts

Object Oriented Programming

It’s an imperative programming paradigm.

Common programming paradigms include:

Important OOPS concepts:

Noob understanding of these things - here

OOPs Design principles

  1. KISS - “Keep It Simple, Stupid”
  2. DRY - “Don’t Repeat Yourself”
  3. YAGNI - “You Ain’t Gonna Need It”
  4. SOLID -
    • Single Responsibility Principle - A class should have one and only one reason to change, meaning that a class should have only one job.
    • Open/Closed Principle - Objects or entities should be open for extension but closed for modification.
    • Liskov Substitution Principle - This means that every subclass or derived class should be substitutable for their base or parent class. It means that the sub classes should extend the functionality of the super class without overriding it.
    • Interface Segregation Principle - Interfaces should be specific rather than doing many and different things. A client should never be forced to implement an interface that it doesn’t use, or clients shouldn’t be forced to depend on methods they do not use.
    • Dependency Inversion Principle - Try to minimize the dependency between objects by using abstraction. Instead of higher level models depending on lower level models, both should depend on abstraction.

    Easy to understand SOLID - best explaination

  5. GRASP - General Responsibility Assignment Software Patterns
    • Information Expert
    • Creator
    • Low Coupling
    • High Cohesion
    • Controller
    • Pure Fabrication
    • Indirection
    • Polymorphism
    • Protected Variations
    • Code Smell
    • Long Method
    • Identifiers
    • Comments
    • The God Object
    • Feature Envy

Refs:

Design Patterns

  1. Structural
  2. Behavioral
  3. Creational

Refs:

Python OOPs implimentation

Underscores in python

_name - internal use variable, does not export when `from file import *`
__name - (name mangling - avoid overriding) make attribute name `_ClassName__name_`
__name__ - (dunder methods)

Private/Protected/Public attributes

Public (objs, children, anyone) - name()
Protected (class and child classes) - _name()
Private (only inside class) - __name()

Class var vs instance var

class Person:
    id = "" # class var
    def __init__(self, name, age):
        # instance var
        self.name = name
        self.age = age
  
    @classmethod # class method
    def fromBirthYear(cls, name, year):
        # a class method to create Person object by birth year.
        return cls(name, date.today().year - year)
  
    def display(self): # instance method
        print("Name : ", self.name, "Age : ", self.age)

    @staticmethod # static method - does not change state 
    def calc_age(birth_year):
        return birth_year - datetime.year 
    
    def __del__(self): # destructor
        print("dying.. byee!")
  
person = Person('mayank', 21)
person.display()

Setters and getters:

__getarr__ (when obj.a is not defined) 
__getarribute__ (everytime when obj.a is called, regardless if its defined or not)
class Name:
    @property  
    def age(self):  
        return self._age
    @age.setter
    def get_age(self, new_age):
        self._age = new_age

Inheritance

super() - access direct parent without explicitly using the parents name

class Child(Parent):
    def __init__(self, a, b):
        super().__init__(a)
        self.b = b

Interfaces and metclass

from abc import ABC, abstractmethod

class NetworkInterface(ABC):
    @abstractmethod
    def connect(self):
        pass
    @abstractmethod
    def transfer(self):
        pass

References