Posts

Identity vs Equality in Python

 This topic is extremely important if you are learning Python as a beginner.  Golden Rule == → compares values is → compares memory identity Example 1: In the following example, even though values are the same , a and b are different objects in memory . a = [1, 2, 3] b = [1, 2, 3] print(a == b)  # True print(a is b)  # False Example 2: In this example, a and b are equal and as well as identical. This is because Python reuses small integers to optimize memory usage which is known as  Integer Interning . a = 256 b = 256 print(a == b) # False print(a is b)  # True Example 3: The same logic applies to string values since Python reuses small string values to optimize memory usage as well. a = "hello" b = "hello" print(a is b)  # True Example 4: In the following example, the values are equal  but they are different objects . a = "".join(["he", "llo"]) b = "hello" print(a is b)  # False

Mutable Default Arguments (A Common Python Bug)

One of the most common mistakes Python developers make is using mutable objects as default function arguments. In this following example, the default list items=[] is used as the function argument. When mutable object - list is used as function argument, the object is created once and reused  every time the function is called. This creates hidden persistent storage , where data keeps accumulating across calls. def add_items(item, items=[] ):     items.append(item)     return items print(add_items(1)) print(add_items(2)) Output [1] [1, 2] "Mutable objects should not be used as default function arguments. Instead, use None  and create the object inside the function." In this way, each function call now creates a fresh new list . def add_items(item, items= None ):     if items is None:         items = []     items.append(item)     return items print(add_items(1)) print(add_items(2)) Output [1] [2]

Python Internals - Mutable vs Immutable Objects

  Understanding Python internals is   crucial for writing bug-free code   and is a   favorite topic in technical interviews . On Day 1, we focus on how Python handles objects in memory, mutability, function arguments, and object identity. Let’s break it down step by step. 1. Mutable vs Immutable Objects in Python In Python,  everything is an object , and each object is either  mutable  or  immutable . 1a. Immutable Objects Immutable objects  cannot be changed after creation . Examples: int float str tuple and frozenset Example: Input a = 10 b = a b += 1 print(a) Output - 10 🧠  This is because   int  is immutable. When  b += 1  is executed, Python creates a  new object  for  b . a  still points to the original value  10 . 1b. Mutable Objects Mutable objects  can be modified in place . Examples: list dict set and  Most Custom Objects Example: Input a = [1, 2, 3] b = a b.append(4) prin...

Distributed Version Control System - Part 2

  To Read - πŸ‘‰πŸ» Distributed Version Control System - Part 1 πŸ‘ˆπŸ» Basic Git Commands 1. Project folder ထတွင်း Git repository α€‘α€žα€…် α€–α€”်တီးခြင်း      git  init 2. α€›ှိပြီးα€žား repository α€€ို local machine α€€ို copy α€œုပ်ခြင်း      git  clone 3. α€œα€€်α€›ှိ file ထခြေထနေ (modified, staged, untracked) α€€ိုα€€ြα€Š့်ခြင်း      git  status 4. ပြင်ဆင်α€™ှုα€™ျားα€€ို commit α€œုပ်α€–ို့ stage α€œုပ်ခြင်း      git  add filename.py        git  add .             5. Staged changes α€€ို repository α€™ှာ save α€œုပ်ခြင်း      git  commit 6. Local repository α€™ှ committed changes α€€ို remote repository α€žို့ ပို့ခြင်း          git  push 7. Remote repository α€™ှ latest changes α€€ို local repository α€žို့ α€šူခြင်း      git  fetch  origin      git  pull  origin main 8. Branch α€™ျားα€€ြα€Š့်ခြင်း α€žို့α€™α€Ÿု...

Distributed Version Control System - Part 1

To Read -  πŸ‘‰πŸ»  Distributed Version Control System (Part 2) - Git Commands  πŸ‘ˆπŸ» Distributed Version Control System Distributed Version Control System (DVCS) ဆိုတာ source code တွေα€€ို တစ်α€”ေα€›ာထဲα€™ှာ α€žိα€™်းα€†α€Š်းတာတင်α€žာမကပဲ၊ ပြင်ဆင်α€™ှုတိုင်းα€€ို α€™ှတ်တမ်းတင်ပေးပြီး ထဖွဲဝင်α€™ျားထကြား ပူးပေါင်းα€œုပ်ဆောင်α€”ိုင်α€…ေတဲ့ α€…α€”α€…်α€–ြα€…ိပါα€α€š်။  Distributed Version Control System (Git) - Git Hub, Git Lab, Bit Bucket, Azure Repos, ... VCS applications တွေα€€ source code α€€ို α€žိα€™်းα€†α€Š်းပေးα€›ုံα€žာα€™α€€ ထောα€€်α€–ော်ပြပါ features supportတွေα€€ို α€œုပ်ပေးပါα€α€š်။ ✨ Repository  Developers တွေα€Ÿာ repository α€œိုခေါာတဲ့ α€žိုα€œှောင်α€›ုံα€™ျားα€…ွာα€€ို α€–α€”်တီးα€”ိုင်ပြီး α€€ိုα€š်ပိုင် project တွေα€€ို α€žိα€™်းα€†α€Š်းထားα€œို့ရပါα€α€š်။ ✨ Collaboration  α€€ိုα€š်α€–α€”်တီးထားတဲ့ repository တွေα€€ို တခြား developers တွေα€€ို sharing α€œုပ်α€”ိုင်α€žα€œို project တစ်ခုထဲ ထတူα€›ေးα€”ေတဲ့ developer team members တွေα€€ို invite α€œုပ်ပြီး α€›ေးထားတဲ့ source code တွေα€€ို repository ပေါာ ထချိα€”်α€”ဲ့တပြေးα€Šီ တင်ခြင်းထားα€–ြင့် team တစ်ခုα€œုံးα€€ α€›ေးထားတဲ့ codingတွေα€Ÿာ ထမြဲ update α€–ြα€…်α€”ေα€™ှ...

Creational Design Pattern - Factory Pattern

Image
In my previous blog, I discussed the Abstract Factory Pattern , which you can read if you're interested. Today, I am going to discuss the Factory Pattern. Factory Design Pattern Factory design pattern is similar to abstract factory pattern and they both are creational design patterns. The only difference is that Factory pattern has only one factory class while abstract factory pattern can have multiple different factories which extend the main abstract factory class. So, if you want to implement an application that have multiple similar objects, you can use factory pattern. Otherwise, if you have to implement multiple different types of objects, you can use abstract factory pattern which allows you to create multiple factories.  For Factory design pattern, it is quite less complex than abstract one. Take a look at this following diagram. I used the same example for both abstract factory and factory pattern. So you can understand the difference between two of them easily. The Factor...

Creational Design Pattern - Abstract Factory

Image
 There are 5 types of creational design patterns - Abstract Factory, Builder, Factory Method, Prototype and Singleton.  Abstract Factory Design Pattern Abstract Factory is used for creation of various types of similar objects. Before explanation about the structure of this pattern, take a look at the following diagram.    The Abstract Factory Pattern may seem complex at first, but once you understand its components, it becomes much easier to work with. Here's a simple breakdown of how it operates. It consists of six key components: Interface : An interface acts as a blueprint that multiple classes will implement. In this example, Food_Drink_Interface defines a food_drink ( ) method with an empty body. This method is implemented by multiple concrete classes such as Burger , Pizza , Coffee , and Tea . Abstract Factory : An abstract factory helps create related objects without needing to know which concrete class is being instantiated. In this case, Food_Drink_Fa...