Search This Blog

Showing posts with label tutorial. Show all posts
Showing posts with label tutorial. Show all posts

Wednesday, 23 July 2025

OOPs in Python

Python supports rich object-oriented programming (OOP) features that promote modularity, scalability, and readability. In this article, we’ll explore core OOP concepts using Python’s syntax and conventions—ideal for building GUI apps, REST APIs, and creative projects.

Classes: They are like a template to create new objects. Similar to the blueprint of a house plan created by the architect.


Constructor: 

Every class has a constructor. This ensures that the object is created and loaded in the application memory with its settings as provided in the constructor. This is similar to the architect of the house instructed the contractor how to lay the foundation with which material and how it needs to be placed etc.

This is done using python's built-in function __init__(self).
This is a special function which will be invoked by python at runtime. You can see the above example with the __init__(self, <parameters>)
where,
self : here indicates the current context of the object created

Inheritance
Inheritance lets one class absorb properties and behaviors of another.


Syntax: 

class Child(Parent):
            ... your code here ...

Let's take the example of senior developer:



** NOTE: Please note, the above class does not have __init__()

Here, we need to understand, that until the parent class is constructed the child class cannot be constructed. Hence order of construction of object in execution, in the case of inheritance is
1.  parent __init__() 
2. child __init__()

CASE: In case the parent constructor has parameters apart from self, then how can these be supplied in inhheritance case?

ANS: Use super().__init__(parent-parameter-values)

Hence the above code will be modified to call the parent class constructor as follows:



Access Specifiers:
The following conventions are used to specify access.


See the following example:
Here instance properties are created where values assigned are native to that specific object only.



Statics:These are shareable variables and function. They are invoked using

ClassName.<static_member>

See the following example:


Overriding: The concept of  changing the behaviour of a function (logic of the function) in the child class is known as overriding




Overloading: The concept of creating a function with the same name but different parameters is known as overloading. Python does not support overloading. This can be done by using *args, **kwargs kind of parametersin the function

Play around with the following trinket showing all the above explained operations as an example



Saturday, 12 June 2021

Symphony with Asynchrony - C#


Let me explain asynchrony to you in a simple way.

Assume there are two functions named Main() and ICanDoAJob(). Main() is a function that executes line-by -line in a sequential manner. If there is a function such as LongRunning() which takes about 2 minutes to complete its execution, then Main() can cause the application to hang for 2min.

Hence Main() decides to give the job of executing LongRunning() to the function ICanDoAJob(), on a condition that ICanDoAJob() will take care of executing LongRunning() without disturbing or interrupting the work execution of Main().

ICanDoAJob(), now works independently, while Main() executes his calls without waiting for LongRunning() until it executes. This is known as asynchrony.


  

How is this Asynchrony achieved?

 

To achieve this asynchrony, let’s understand how a resource is executed.

The OS memory manager allocates memory for a resource to run on memory. When this is done, a Process Id is assigned, and each resource running in memory (RAM) is called as a process. Process is usually a background service or an executable (.exe). For example, when you open Visual Studio, a process is created for its underlying exe which is “devenv.exe”. This can be viewed in the Task Manager (of Windows OS), as shown in the screenshot (A) below. You will observe that it is allocated with a ProcessId. Similarly, Each process has atleast one thread known as the Main thread, that executes the process. In a normal program, this is the Main() method. A Thread can be considered as an independently executing task. If the Main() thread now spawns additional such threads which execute asynchronously, and merge with the Main() after completion, then technically this is known as asynchrony. The total threads for a particular process can be seen in the Task Manager -> Performance Tab -> Open Resource Monitor. Please observe the screenshot (B) to understand how many threads are created for the process “devenv.exe



 

(A)                                

 


                                

 

                                                                                                            (B)


                                      

In .Net, if one wants spawn threads from the Main() method, it can be achieved using a class known as the ThreadStart() delegate. This is highlighted in the code block below.

1.  Thread thr = new Thread(new ThreadStart(() => {
2.                                          for (int i = 0; i < 10; i++)
3.                                                         Console.WriteLine($"Thread {i}");
4.                                       }));
5.              thr.Start();
6.   

Here, the developer needs to take care of thread management efficiently through code. As threads run independently, this can be a problem if the thread is not stopped or merged correctly.

With the advent of new features introduced in .Net the concept of “Tasks” is introduced with a garnish of two best friends introduced in the form of keywords “async … await”. Async & await can be understood by the analogy of husband (async) and wife (await). The wife brings in order and creates a disciplined routine. Similarly, await will see that a task running asynchronously runs in an ordered manner. When async is used without await, the execution will be in a haphazard manner. The wife is always with the husband. Hence, await (wife) cannot be used if a method is not marked as async (husband).

Hence technically, there is a class in the System.Threading namespace, called Task.

The unit of code (An anonymous Lambda / a named function) to be executed asynchronously is created as a Task. To mark this unit of code to run asynchronously, the method is marked as “async”.

If there are multiple such tasks executing independently, where the output of one TaskA is an input to TaskB, then it is important to await the completion of the execution of TaskA, before passing its output to TaskB. Here it is important to use the keyword “await”.

Is it important to use await with async? Can async be used without await? The quick answer is YES.

But, unless you mark a method as async, await cannot be used. This means, to use await, a method must be marked as async. But if a method is marked as async, await may or may not be added.

The syntax for creating a Task is as follows

1.  Task.Run(()=>{});
2.   

In the above code, Run is a function, that takes Action delegate as a parameter. Let’s now create a function that uses asyncawait and Task. The following program will print the text on the console.

1.  static void Main(string[] args)
2.          {
3.             Method1();
4.          }
5.  public static void Method2()
6.          {
7.              for (int i = 0; i < 25; i++)
8.              {
9.                  Console.WriteLine($"M2-{i} Method 2");
10.               }
11.           }
12.   public static async void Method1()
13.           {
14.               await Task.Run(() =>
15.               {
16.                   for (int i = 0; i < 1000; i++)
17.                   {
18.                       Console.WriteLine($"M1-{i} Method 1");
19.                   }
20.               });
21.  

The same can be done using a TaskFactory, which creates tasks from a pool of tasks.

1.              TaskFactory factory = new TaskFactory();
2.              factory.StartNew(() => Method2());
3.              factory.StartNew(() => Method3(9999));
4.   
5.              Task.Factory.StartNew(() => Method2());
6.   

Tasks can also be pre-created and stored in a collection. In this case, it is better to use a thread-safe collection, such as ConcurrentBag<>. To do so, we create Task objects and add them to a ConcurrentBag, and then execute it wherever required. Have a look at the code below.

1.  public static async void UseConcurrentBag()
2.          {
3.   
4.              ConcurrentBag<Task> taskBag = new ConcurrentBag<Task>();
5.              Task t1 = new Task(() => Method1());
6.              Task t2 = new Task(() => Method2());
7.   
8.              taskBag.Add(t1);
9.              taskBag.Add(t2);
10.    
11.               foreach (var t in taskBag)
12.               {
13.                   t.Start();
14.                   Console.WriteLine($"Id: {t.Id}, Status: {t.Status}, IsCancellable: {t.IsCanceled}");
15.               }
16.           }
17.    

The difference between a thread and a Task is, a task can be cancelled midway. This is done by adding a CancellationToken at the time of creation of the task. This CancellationToken object is generated from a CancellationTokenSource class object. View the sample code below.

1.  public static void UsingTaskCancellation()
2.          {
3.              //STEP 1: Initialize Cancellation token source to cancel a task
4.              CancellationTokenSource tokenSource = new CancellationTokenSource();
5.              CancellationToken token = tokenSource.Token;
6.              
7.              //Start a new task by passing the cancellation token
8.              Task t = Task.Factory.StartNew(() => {
9.                  int counter = 1;
10.                   while (!token.IsCancellationRequested)
11.                   {
12.                       Console.WriteLine(counter++);
13.                       Thread.Sleep(1000);
14.                   }
15.               }, token);
16.    
17.               //On any key pressed by user, cancel the task
18.               Console.WriteLine("======== Press any key to cancel this task ========");
19.               Console.ReadKey();
20.               tokenSource.Cancel();   //Cancels a running task
21.               Console.WriteLine("Task Cancelled");
22.           }
23.    

 Hope you get a headstart to asynchrony and enjoy your programming journey ahead.